Ruby on Rails
PhpMcryptFunctions

You can use OpenSSL to mimic the functionality of mencrypt. If you are decrypting PHP mcrypt encoded items, the key is null padded to max keysize returned by mcrypt_enc_get_key_size(). Below are “compatibility mode” examples. These can be passed back and forth between PHP and Ruby using the same key to decrypt/encrypt the data.

To encrypt:


require 'openssl'
cipher = OpenSSL::Cipher::Cipher.new("des-ede3-cbc")
cipher.encrypt
key = "my secret key" 

_#TripleDES uses 26 character key_
key << "\0" until key.length == 26

cipher.key = key
cipher.iv = iv_to_use = cipher.random_iv    

data_to_encode = "Encode me" 
encoded_data = cipher.update(data_to_encode)
encoded_data << cipher.final
puts encoded_data

To decrypt:


require 'openssl'
cipher = OpenSSL::Cipher::Cipher.new("des-ede3-cbc")
cipher.decrypt
key = "my secret key" 

_# TripleDES uses 26 character key_
key << "\0" until key.length == 26

cipher.key = key
cipher.iv = iv_to_use

decoded_output = cipher.update( data_to_decrypt )
decoded_output << cipher.final
puts decoded_output