我使用OpenSSL进行以下加密/解密(在我的示例中在Linux下):
$ echo test | openssl des -a -K 79616d7379616d73 -iv 1234567890ABCDEF
+ax5NT+Pjh0=
$ echo +ax5NT+Pjh0= | openssl des -a -d -K 79616d7379616d73 -iv 1234567890ABCDEF
test
一切都好。我需要在Ruby代码中翻译它。就我而言:
#!/usr/bin/env ruby
require 'openssl'
key = "\x79\x61\x6d\x73\x79\x61\x6d\x73"
iv = "\x12\x34\x56\x78\x90\xAB\xCD\xEF"
todecode = "+ax5NT+Pjh0="
def decode(encryptedString, key, iv)
decrypt = OpenSSL::Cipher::Cipher.new('des-cbc')
decrypt.decrypt
decrypt.key = key
decrypt.iv = iv
decrypt.update(encryptedString) + decrypt.final
end
decoded = decode(todecode, key, iv)
puts decoded
它引发了以下错误:
decode.rb:14:in `final': wrong final block length (OpenSSL::Cipher::CipherError)`
我做错了什么?我选择了错误的加密或错误使用key / iv吗?
答案 0 :(得分:0)
似乎我忘记了base64_decode字符串。
todecode = Base64::decode64("+ax5NT+Pjh0=")