Cryptography (RSA)

I trying to do some cryptography between ruby and php, I don’t get
it!!!

ruby code:

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

text = “abcdefghijklmnopqrstuvwxyz”
key = “altakey”
alg = “AES-128-ECB”

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

This is the result:
“\214\t\303n\320Lz\330\271\252\017\355\036\251|\237\212V\270hq\267X\204\261\3327t\345\353\324\364”

what is that? \214???\t\303n?

this is the php code:

<?php $crypt = '\214\t\303n\320Lz\330\271\252\017\355\036\251|\237\212V\270hq\267X\204\261\3327t\345\353\324\364'; $key = "altakey"; echo "crypted content from ruby: " .$crypt . "\n"; $result = mcrypt_decrypt ( MCRYPT_RIJNDAEL_128 , $key, $crypt, MCRYPT_MODE_ECB); echo "result: $result\n" ?>

this is the result:
ãå�dg[0l?�gª�¤½C¶(Ayy2�zeõt
A�e:vb¶Uu<²é%fi­czÏ�¯

wtf???

by the way, this is a code to encrypt in php

<?php $text = "abcdefghijklmnopqrstuvwxyz"; $key = "altakey"; echo "decrypted content: $text\n"; echo "key: $key\n"; $result = mcrypt_encrypt ( MCRYPT_RIJNDAEL_128 , $key, $text, MCRYPT_MODE_ECB); echo "Encrypted text: " . bin2hex($result) . "\n"; ?>

this is the result:
1fce1c90530b80a27b1af29364ba45759870f1a2341c380dcc48845335d9e575

how can I manage that hex data in ruby? any help on anything?

the title should be: Cryptography (AES)
sorry

Rodrigo D. schrieb:

alg = “AES-128-ECB”
puts %(encrypted text: #{cipher.inspect})

<?php
?>

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

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

thank you very much for the answer, I didn’t know about the key length
and the iv vector, I will try again, thank you

On Monday, September 25, 2006, at 09:55AM, Rodrigo D.
[email protected] wrote:

des.iv = iv.
I bet google shows enough examples how to use openssl in ruby the right
way.

Hello,
I am coming into this conversation in the middle, but a quick googling
found the following discussion.

In this thread they talk about ruby openssl expecting the key to come in
as a binary data, not a simple string of hex digits. I’ll paste a
relevant quote from the linked thread…

####BEGIN QUOTE HERE####
"
binary_data = unpack(‘a2’*32).map{|x| x.hex}.pack(‘c’*32)

The “unpack” call assumes you have 32 pairs of hex digits (the ‘a2’ bit
will match a two digit hex number). Change the ‘32’ to match however
many hex numbers are in your string. The “map” call creates an array
from the unpacked string, with the 32 two-digit hex numbers converted
into 32 decimal numbers. The “pack” call takes this array of 32 integers
and converts it into a packed stream of 32 bytes."
####END QUOTE HERE####

Hope this helps somewhat.
Patrick

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);