user812120
user812120

Reputation: 585

Ruby on Rails Decryption

The following function works perfect in PHP. How can it be translated in Ruby on Rails.

Please note that both privateKey and iv are 32 characters long.

mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $privateKey, base64_decode($enc), MCRYPT_MODE_CBC, $iv)

I tried to use the following in Ruby but got a bad decrypt error.

cipher = OpenSSL::Cipher.new('aes-256-cbc')

cipher.decrypt
cipher.key = privateKey
cipher.iv = iv

decrypted = '' << cipher.update(encrypted) << cipher.final

Upvotes: 0

Views: 2448

Answers (2)

CupraR_On_Rails
CupraR_On_Rails

Reputation: 2489

Here some code which works for me :


def decrypt_data(data, pwd, iv)
    encrypted_data = Base64.decode64(data)
    aes = OpenSSL::Cipher::Cipher.new("AES-256-CBC")
    aes.decrypt
    aes.key = Digest::MD5.hexdigest(pwd)
    aes.iv = iv
    result = aes.update(encrypted_data) + aes.final 
end

In my example the password is encrypted with MD5.

I hope this help

Upvotes: 1

Sionide21
Sionide21

Reputation: 2211

You are base64 decoding it in the php example. Are you doing that in the ruby one as well?

require "base64"
Base64.decode64(encrypted)

Other than that, the code looks right to me.

Upvotes: 0

Related Questions