cryptography (RSA)

R

Rodrigo Dominguez

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


?>
 
R

Roland Schmitt

Rodrigo said:
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";


?>
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
 
R

Rodrigo Dominguez

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
 
R

Rodrigo Dominguez

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

Patrick Toomey

Hello,
I am coming into this conversation in the middle, but a quick googling found the following discussion. http://www.bigbold.com/snippets/posts/show/576
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top