【发布时间】:2015-03-29 21:50:10
【问题描述】:
我一直在尝试将用于在 java 中加密的代码转换为 ruby,但我无法完全做到这一点。我得到不同的值。
passphrase = passphrase + STATIC_KEY;
byte[] key = passphrase.getBytes("UTF-8");
MessageDigest sha = MessageDigest.getInstance("SHA-1");
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
SecretKey secretKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
IvParameterSpec initialisationVector = new IvParameterSpec(
new byte[16]);
cipher.init(Cipher.ENCRYPT_MODE, secretKey, initialisationVector);
byte[] encryptedData = cipher.doFinal(plainText.getBytes("UTF-8"));
return SimpleCrypto.toHex(encryptedData);
谁能告诉我,如何在 ruby 中做到这一点。
unencrypted = "passphrase"
c = OpenSSL::Cipher.new("aes-128-cbc")
c.encrypt
c.key = Digest::SHA1.hexdigest('secret_key')[0...32]
e = c.update(unencrypted)
e << c.final
return e
【问题讨论】:
-
如果我的回答正确,请告诉我
-
@railrailrail,我已经发布了我的答案。我试过你的代码。
hex_to_bin密钥转换是缺失的部分。请检查我的答案。
标签: java ruby ruby-on-rails-3 aes sha