0

I have the following code in Python that works well. It decrypts the data in the variable encrypted_data using the mcrypt module. I want to perform the same operations in Ruby. Therefore I required the openssl module. Unfortunately the Ruby version does not work. This is the error:

`final': bad decrypt (OpenSSL::Cipher::CipherError)

EDIT I created a GitHub repository for you to test this. Download the sourcecode here: github.com/ph3nx/ruby-decrypt

EDIT 2 The bad decrypt error does not appear anymore. I fixed this by setting d.padding = 0. Problem is: the decrypted data is still not correct. I can't view the image.

If you need additional Information about my system or anything else feel free to ask. Any help is highly appreciated. Thanks!

Python

import base64
import mcrypt

encrypted_data = "placeholder: This is the encrypted data."

key = base64.b64decode("6vYJkO5beHNlwOm+aMqUTTzSpRw9jR3faHqu0wVoAG0=")
iv = base64.b64decode("EbuuKuVF4+DzDbWsvQi/ZA==")

m = mcrypt.MCRYPT("rijndael-128", "cbc")
m.init(key, iv)
decrypted_data = m.decrypt(encrypted_data)

Ruby

require 'base64'
require 'openssl'

encrypted_data = "placeholder: This is the encrypted data."

key = Base64.decode64("6vYJkO5beHNlwOm+aMqUTTzSpRw9jR3faHqu0wVoAG0=")
iv = Base64.decode64("EbuuKuVF4+DzDbWsvQi/ZA==")

d = OpenSSL::Cipher::AES128.new :CBC
d.decrypt
d.padding = 0
d.key = key
d.iv = iv
decrypted_data = d.update(encrypted_data) << d.final
7
  • Decryption is not decoding. Ciphertext contains random bytes so it should not be converted directly to/from a string. Commented Mar 7, 2014 at 2:30
  • What is your encrypted data source? Is it from Python? Commented Mar 7, 2014 at 9:22
  • @owlstead Yes, you are right! The program derypts data. I changed the code accordingly. Commented Mar 7, 2014 at 9:24
  • @tsundoku The data is loaded from a different server. I don't know which programming language is used to encrypt the data. Why is this important? Commented Mar 7, 2014 at 9:26
  • There are a couple of settings to tweak, e.g. if you set d.padding = 0, from here: stackoverflow.com/questions/19661508/… Commented Mar 7, 2014 at 9:31

1 Answer 1

1
+50

I have found OpenSSL integration to be lacking in Ruby, and generally more cumbersome (uglier api / less convention over configuration) for cryptographic work then mcrypt.

If you're using mcrypt in python successfully, why not use mcrypt in ruby too? https://github.com/kingpong/ruby-mcrypt

Something in the lines of:

require 'rubygems'
require 'mcrypt'
crypto = Mcrypt.new("rijndael-128", :cbc, key, iv)
decrypted_data = crypto.decrypt(encrypted_data)

should work in your case.

I can extend on this answer later today or tomorrow, got some traveling to do first

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

2 Comments

You're a genius! This works exactly how I wanted. Do you know if there are differences in performance between mcrypt and openssl?
Lol thank you :) I'm glad it helped :) I don't know about performance differences, but they're all C-binded libs so mcrypt performance should be pretty good. We used ruby-mcrypt for encrypting some 10 million records or so in a live system with 700k users and we didn't run into any bottlenecks in performance there

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.