I’m trying to encrypt some text in rails, and to decrypt the text with
php, and to crypt something with php, and decrypt it with ruby, but I
having problems…
I try to encrypt with ruby, php and openssl console command, the same
text, with the same key, and the same algorithm (AES) but I always got
different results, so whenever I try to decode the text with another
language, I got unwished results

ruby code:

#!/usr/bin/env ruby
require ‘openssl’

text = “abcdefghijklmnopqrstuvwxyz”
key = “altakey”
alg = “AES-128-ECB”
file_name = “test.encrypted”
file_name_2 = “test.decrypted”

puts %(clear text: “#{text}”)
puts %(symmetric key: “#{key}”)
puts %(cipher alg: “#{alg}”)

puts “–Encrypting–”
des =
cipher = des.update(text)
cipher <<
puts %(encrypted text: #{cipher.inspect})

file =, “w”)
file << cipher

The result:

<8C> �nÐLz�¹ª^Oí^^©|<9F><8A>V¸hq·X<84>±�7tåë�ô

This is the php code:

$text = “abcdefghijklmnopqrstuvwxyz”;
$key = “altakey”;
$alg = “AES-128-ECB”;
$file_name = “test.encrypted”;
$file_name_2 = “test.decrypted”;

echo “decrypted content: $text\n”;
echo “key: $key\n”;

$result = mcrypt_encrypt ( MCRYPT_RIJNDAEL_128 , $key, $text,

echo “result: $result\n”;

$file = fopen($file_name, “w”);
fwrite($file, $result);

The result (with a warning):
Warning: mcrypt_encrypt(): Attempt to use an empty IV, which is NOT
recommend in
/usr/local/src/ruby/ruby-1.8.4/sample/openssl/test.encrypt.php on line


And finally, an openssl version:

openssl enc -aes-128-ecb -in test.original -out test.encrypted

The result:


They are throw different results, but the encription algorithm is the
same and the key is the same

My suggestion would be to use EXACTLY the same libraries on both ends -
would first use ruby-mcrypt before I went any further

John W Higgins
[email protected]

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs