我有一些来自 API 提供商的 testdata 密钥/文本/加密,现在正尝试使用下面的函数产生相同的加密结果,但我的结果偏离了提供的 241 位数字中的最后 16 位。你有什么想法,可能是什么原因?我确保“bf-ecb”是正确的模式,并尝试了 url 编码,但到目前为止没有成功。
require 'openssl'
def encrypt(key, data)
cipher = OpenSSL::Cipher::Cipher.new('bf-ecb').send(:encrypt)
cipher.key = key
result = cipher.update(data) << cipher.final
hexed = ''
result.each_byte { |c| hexed << '%02x' % c }
hexed.upcase
end
更新
还尝试解密示例结果会导致 OpenSSL::Cipher::CipherError "bad decrypt"