1

I have the following encryption/decryption using OpenSSL (under Linux in my example):

$ echo test | openssl des -a -K 79616d7379616d73 -iv 1234567890ABCDEF
+ax5NT+Pjh0=

$ echo +ax5NT+Pjh0= | openssl des -a -d -K 79616d7379616d73 -iv 1234567890ABCDEF
test

All good. I need to translate it in Ruby code. As far as I did:

#!/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

It throws me the following error:

decode.rb:14:in `final': wrong final block length (OpenSSL::Cipher::CipherError)`

What am I doing wrong? Did I select the wrong encryption or wrong use of key/iv?

1
  • 1
    Small nit... Use echo -n, not echo (unless you want the new line). But it probably is not the cause of your problem. Commented May 28, 2017 at 23:22

1 Answer 1

1

Seems I forgot to base64_decode the string.

todecode = Base64::decode64("+ax5NT+Pjh0=")

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.