gpt4 book ai didi

javascript - JS/Ruby AES 256 对称性

转载 作者:数据小太阳 更新时间:2023-10-29 08:24:43 24 4
gpt4 key购买 nike

我正在使用 Ruby 加密并使用 AES-256 通过 JavaScript 解密,但我在跨平台时遇到问题,即在解密 Ruby 的输出时 JS 返回乱码。

我正在使用此处的 JS AES 实现:http://www.movable-type.co.uk/scripts/aes.html

var decrypted = Aes.Ctr.decrypt(encrypted, key, 256);

和 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

在 ruby 中我得到:

encrypt("This is a test", "password")
# => "zDMm47GniTQ2p5a5UqSDbg=="

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

Aes.Ctr.decrypt("zDMm47GniTQ2p5a5UqSDbg==", "password", 256);
# => "Ü}$> 3"

有什么想法吗?提前致谢。

最佳答案

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

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

https://github.com/mdp/gibberish

关于javascript - JS/Ruby AES 256 对称性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8080525/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com