gpt4 book ai didi

javascript - Ruby openssl 中的 AES 等价物?

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

Gibberish 库提供了一个很好的 CBC 算法...

// In Jascascript
GibberishAES.enc("Made with Gibberish\n", "password");
// Outputs: "U2FsdGVkX1+21O5RB08bavFTq7Yq/gChmXrO3f00tvJaT55A5pPvqw0zFVnHSW1o"

# On the command line
echo "U2FsdGVkX1+21O5RB08bavFTq7Yq/gChmXrO3f00tvJaT55A5pPvqw0zFVnHSW1o" |
openssl enc -d -aes-256-cbc -a -k password

如何在 ruby​​ 中进行解密?直截了当的方法行不通...

require 'openssl'

def aes(m,k,t)
(aes = OpenSSL::Cipher::Cipher.new('aes-256-cbc').send(m)).key = k
aes.update(t) << aes.final
end

def encrypt(key, text)
aes(:encrypt, key, text)
end

def decrypt(key, text)
aes(:decrypt, key, text)
end

def p k
Digest::SHA256.digest(k) ## what goes here???
end

require 'base64'
def t x
## also tried.. simply returning x...
Base64.decode64(x)
end


text = "U2FsdGVkX1+21O5RB08bavFTq7Yq/gChmXrO3f00tvJaT55A5pPvqw0zFVnHSW1o"
pass = "password"

decrypt(p(pass), t(text))

最佳答案

深入研究乱码...提供了答案的线索。以及为什么传统机制不起作用。

dec = function(string, pass) {
// string, password in plaintext
var cryptArr = Base64.decode(string),
salt = cryptArr.slice(8, 16),
pbe = openSSLKey(s2a(pass), salt),
key = pbe.key,
iv = pbe.iv;
cryptArr = cryptArr.slice(16, cryptArr.length);
// Take off the Salted__ffeeddcc
string = rawDecrypt(cryptArr, key, iv);
return string;
},

现在转换为 ruby​​ 非常简单。记下它以供我个人将来引用。

require 'base64'
require 'openssl'

def decode(k,t)
cryptArr = Base64.decode64(t)
salt = cryptArr[8..15]
data = cryptArr[16..-1]

aes = OpenSSL::Cipher::Cipher.new('AES-256-CBC').decrypt
aes.pkcs5_keyivgen(k, salt, 1)
s = aes.update(data) + aes.final
end

orig = "Made with Gibberish\n"
cipr = "U2FsdGVkX1+21O5RB08bavFTq7Yq/gChmXrO3f00tvJaT55A5pPvqw0zFVnHSW1o"
pass = "password"

puts decode(pass, cipr)

关于javascript - Ruby openssl 中的 AES 等价物?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1938430/

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