Created
November 3, 2017 14:03
-
-
Save vivahiraj/bdd715df10ee3298a79b4ef503c48386 to your computer and use it in GitHub Desktop.
JavaScriptで var text = CryptoJS.AES.decrypt(data, "pass").toString(CryptoJS.enc.Utf8); とすれば、複合できるようにするためのRubyでの暗号化方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "openssl" | |
require "base64" | |
def encrypt(data, passwd) | |
enc = OpenSSL::Cipher.new("AES-256-CBC") | |
enc.encrypt | |
salt = OpenSSL::Random.random_bytes(8) | |
enc.pkcs5_keyivgen(passwd, salt, 1) | |
enc_data = enc.update(data) + enc.final | |
ret = "Salted__" + salt + enc_data | |
ret = Base64.encode64(ret).encode("utf-8").chomp | |
return ret | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment