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 = OpenSSL::Cipher::Cipher.new(alg)
des.encrypt(key)
cipher = des.update(text)
cipher << des.final
puts %(encrypted text: #{cipher.inspect})
puts
file = File.open(file_name, "w")
file.truncate(0)
file << cipher
file.close
---------------------------------------
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,
MCRYPT_MODE_ECB);
echo "result: $result\n";
$file = fopen($file_name, "w");
fwrite($file, $result);
fclose($file);
----------------------------------------
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
13
----------------------------------------
^_Î^\<90>S^K<80>¢{^Zò<93>dºEu<98>pñ¢4^\8^MÌH<84>S5Ùåu
----------------------------------------
And finally, an openssl version:
----------------------------------------
#!/bin/bash
openssl enc -aes-128-ecb -in test.original -out test.encrypted
----------------------------------------
The result:
----------------------------------------
Salted__9Y<80>gí^LÕ<96>}Å®4^FÏÑlÊÃÅ&Àß^Sª&÷ë)±ú9i*ßVáI<91>ê
----------------------------------------
They are throw different results, but the encription algorithm is the
same and the key is the same
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---