JS/Ruby AES 256对称性

oni*_*ons 2 javascript ruby encryption aes

我正在使用Ruby进行加密并使用AES-256使用JavaScript进行解密,但是我遇到了跨平台的麻烦,即JS在解密Ruby的输出时会返回乱码.

我在这里使用JS AES实现:http://www.movable-type.co.uk/scripts/aes.html

var decrypted = Aes.Ctr.decrypt(encrypted, key, 256);
Run Code Online (Sandbox Code Playgroud)

和OpenSSL/Ruby:

def encrypt(string, key)
  Base64.encode64(aes(key, string)).gsub /\s/, ''
end

def aes(key,string)
  cipher = OpenSSL::Cipher::Cipher.new("aes-256-cbc")
  cipher.encrypt
  cipher.key = Digest::SHA256.digest(key)
  cipher_text = cipher.update(string)
  cipher_text << cipher.final
  return cipher_text
end
Run Code Online (Sandbox Code Playgroud)

在红宝石中,我得到:

encrypt("This is a test", "password")
# => "zDMm47GniTQ2p5a5UqSDbg=="
Run Code Online (Sandbox Code Playgroud)

但是当在JS中解密时,我得到:

Aes.Ctr.decrypt("zDMm47GniTQ2p5a5UqSDbg==", "password", 256);
# => "Ü}$> 3"
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?提前致谢.

oni*_*ons 6

最后我使用了Gibberish AES,它同时由同一作者编写的Ruby和JavaScript实现:

https://github.com/mdp/gibberish-aes

https://github.com/mdp/gibberish