1

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)

Thanking you very much in anticipation

1

2 Answers 2

1

Try ruby-mcrypt gem.

require 'rubygems'
require 'mcrypt'

crypto = Mcrypt.new(:twofish, :cbc, MY_KEY, MY_IV, :pkcs)

# encryption and decryption in one step
ciphertext = crypto.encrypt(plaintext)
plaintext  = crypto.decrypt(ciphertext)
Sign up to request clarification or add additional context in comments.

Comments

-1

Use only the specific classes that have those functions.

5 Comments

Many thanks for your quick response. I am new to Ruby on Rails. Could you please throw some more light on it?
What gems do I need to install?
I have run the following commands at irb command prompt and they return true. require "base64" require 'hmac-md5' require 'crypt/rijndael' When I use the following mcrypt_decrypt(MCRYPT_RIJNDAEL_256, privateKey, Base64.decode64(enc), MCRYPT_MODE_CBC, iv) I get an error uninitialized constant ActionView::CompiledTemplates::MCRYPT_RIJNDAEL_256
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
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.