0

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
1
  • 1
    You're not using this for a password, right? Commented Apr 11, 2012 at 14:10

2 Answers 2

1

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

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

9 Comments

Many thanks for your quick response. However, this does not seem to work for me. I am new to Ruby on Rails. I will be very grateful if you could please tell me how to get the same results in Ruby on Rails as the following php code produces in php
If I remove + aes.final from the above ruby code, I get a string but it is not readable. I I do not remove + aes.final, I get "bad decrypt" error
I found this in php doc php.net/manual/fr/function.mcrypt-decrypt.php As you can see, the first comment sais "MCRYPT_RIJNDAEL_256 is not equivalent to AES_256". This is probably a way...
MD5 does not encrypt, it hashes.
I am still not getting the result the php code produces in php :( I am really stuck with this
|
0

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.

2 Comments

Many thanks for your quick response Here is the complete PHP code $iv = base64_decode($_REQUEST["bpi"]); $enc = $_REQUEST["bpe"]; $sig = $_REQUEST["bps"]; $sig2 = hash_hmac("sha256", utf8_encode($iv . '.' . $enc), utf8_encode($privateKey)); echo(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $privateKey, base64_decode($enc), MCRYPT_MODE_CBC, $iv)); It works fine in PHP But the above Ruby code gives me a bad decrypt error. I am base64 decoding in ruby as well
Is there a Ruby equivalent of MCRYPT_RIJNDAEL_256? If I use MCRYPT_RIJNDAEL_256 in PHP and AES-256-CBC in Ruby to encrypt the same data using the same key (32 characters) and iv (32 characters) and then base64 encode the encrypted data, I always get different results. However, if I use MCRYPT_RIJNDAEL_128 in php and AES-256-CBC in Ruby using a key (32 characters) and iv (16 characters) and then base64 encode the encrypted data, I get the same results.

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.