2012-09-20 74 views
4

執行cipher.final時,它說bad decrypt錯誤。我試圖找出問題所在。但是,我找不到。你能告訴我的代碼有什麼問題嗎?ruby​​壞解密錯誤

這裏是我的代碼:

require 'openssl' 
require 'base64' 
require 'hex_string' 

result_h ="4fcd6b1ac843a2f8bf13f2e53dd5c1544fcd6b1ac843a2f8" 
key = result_h.to_byte_string 

encrypt_str="79994A6EF73DA76C"; 
cipher = OpenSSL::Cipher.new("DES-EDE3-CBC") 
cipher.decrypt 
cipher.key = key 
data = encrypt_str.to_byte_string 
res = cipher.update(data) 
res << cipher.final 
result_h= res.unpack("H*")[0] 
puts result_h.inspect; 

錯誤是:

in `final': bad decrypt (OpenSSL::Cipher::CipherError) 
+0

我也處於同樣的情況。 – Shunter1112

回答

7

我也有類似的問題。在具體的例子中:

require 'openssl' 
require 'base64' 
require 'hex_string' 

result_h ="4fcd6b1ac843a2f8bf13f2e53dd5c1544fcd6b1ac843a2f8" 
key = result_h.to_byte_string 

encrypt_str="79994A6EF73DA76C"; 
cipher = OpenSSL::Cipher.new("DES-EDE3-CBC") 
cipher.decrypt 
cipher.padding = 0 
cipher.key = key 
data = encrypt_str.to_byte_string 
res = cipher.update(data) 
res << cipher.final 
result_h= res.unpack("H*")[0] 
puts result_h.inspect; 

=> "0befe932733d76e6" 

如果你添加cipher.padding = 0它會給你一個結果。