> Hi, > > i think that you used the openssl-ruby-bindings in a wrong way. > First, to set the encryption key, you have to use > des.key = key (not des.encrypt(key)). > Second, when using a 128-bit Algorithm, your key must be 128-bit. > Your key is only 8*7=56 bit. > Third, the encryption algorithms not only need the same key, they also > need the same iv (initialisation vector) to show the same results. > In ruby it is > des.iv = iv. > I bet google shows enough examples how to use openssl in ruby the right > way. > > Regards, > Roland I did as you say, and I'm still having different results... I'm really lost If I encrpyt anything with ruby and decrypt it with ruby, it works great, the same with php, but if I encrypt anything with ruby and try to decrypt it with php, it doesn't work ruby code for encryption ----------------------------------- require 'openssl' text = "abcdefghijklmnopqrstuvwxyz" key = "1234567890123456" alg = "AES-128-ECB" iv = "6543210987654321" file_name = "ruby.encrypted" file_name_2 = "ruby.decrypted" puts %(clear text: "#{text}") puts %(symmetric key: "#{key}") puts %(initialization vector: "#{iv}") puts %(cipher alg: "#{alg}") puts "--Encrypting--" des = OpenSSL::Cipher::Cipher.new(alg) des.encrypt des.key = key des.iv = iv cipher = des.update(text) cipher << des.final puts %(encrypted text: #{cipher}) puts file = File.open(file_name, "w") file.truncate(0) file << cipher file.close ----------------------------------- ruby code for decryption ----------------------------------- require 'openssl' text = "abcdefghijklmnopqrstuvwxyz" key = "1234567890123456" alg = "AES-128-ECB" iv = "6543210987654321" file_name = "ruby.encrypted" file_name_2 = "ruby.decrypted" puts %(clear text: "#{text}") puts %(symmetric key: "#{key}") puts %(initialization vector: "#{iv}") puts %(cipher alg: "#{alg}") file = File.open(file_name, "r") text = file.read(999999) file.close puts "--Decrypting--" des = OpenSSL::Cipher::Cipher.new(alg) des.decrypt des.key = key des.iv = iv out = des.update(text) out << des.final puts %(decrypted text: "#{out}") puts file = File.open(file_name_2, "w") file.truncate(0) file << des.final file.close ----------------------------------- php code for encryption ----------------------------------- $text = "abcdefghijklmnopqrstuvwxyz"; $key = "1234567890123456"; $alg = "rijndael-128"; $iv = "6543210987654321"; $file_name = "php.encrypted"; $file_name_2 = "php.decrypted"; $mode = "ecb"; echo("clear test: $text\n"); echo("symmetric key: $key\n"); echo("initialization vector: $iv\n"); echo("cipher alg: $alg\n"); $td = mcrypt_module_open($alg, NULL, $mode, NULL); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), $iv); mcrypt_generic_init($td, $key, $iv); $result = mcrypt_generic($td, $text); mcrypt_generic_deinit($td); mcrypt_module_close($td); echo "result: $result\n"; $file = fopen($file_name, "w"); fwrite($file, $result); fclose($file); ----------------------------------- php code for decryption ----------------------------------- $text = "abcdefghijklmnopqrstuvwxyz"; $key = "1234567890123456"; $alg = "rijndael-128"; $iv = "6543210987654321"; $file_name = "php.encrypted"; $file_name_2 = "php.decrypted"; $mode = "ecb"; $file = fopen($file_name, "r"); $text = fread($file, filesize($file_name)); fclose($file); echo("clear test: $text\n"); echo("symmetric key: $key\n"); echo("initialization vector: $iv\n"); echo("cipher alg: $alg\n"); $td = mcrypt_module_open($alg, NULL, $mode, NULL); $iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), $iv); mcrypt_generic_init($td, $key, $iv); $result = mdecrypt_generic($td, $text); mcrypt_generic_deinit($td); mcrypt_module_close($td); echo "result: $result\n"; $file = fopen($file_name_2, "w"); fwrite($file, $result); fclose($file); ----------------------------------- -- Posted via http://www.ruby-forum.com/.