Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bad decrypt error in ruby

While executing the cipher.final, It says bad decrypt error. I tried to find out the problem.But,I can't find . Can you tell what is wrong with my code?

Here is my code:

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;

Error is:

in `final': bad decrypt (OpenSSL::Cipher::CipherError)
like image 660
sat Avatar asked Sep 20 '12 08:09

sat


1 Answers

I had a similar problem. In the specific example:

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"

If you add cipher.padding = 0 it gives you a result.

like image 198
Giannis Avatar answered Oct 22 '22 07:10

Giannis