# Cryptography (RSA)

**URL:** <https://www.ruby-forum.com/t/cryptography-rsa/69681>\
**Category:** Ruby\
**Created:** [September 22, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/cryptography-rsa/69681 "2006-09-22T23:01:14Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [September 22, 2006, 11:01pm UTC](https://www.ruby-forum.com/t/cryptography-rsa/69681/1 "2006-09-22T23:01:14Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [September 22, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/cryptography-rsa/69681/2 "2006-09-22T23:02:06Z")

</div>

the title should be: Cryptography (AES)  
sorry

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [September 25, 2006, 9:43am UTC](https://www.ruby-forum.com/t/cryptography-rsa/69681/3 "2006-09-25T09:43:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [September 25, 2006, 4:04pm UTC](https://www.ruby-forum.com/t/cryptography-rsa/69681/4 "2006-09-25T16:04:03Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [September 25, 2006, 5:20pm UTC](https://www.ruby-forum.com/t/cryptography-rsa/69681/5 "2006-09-25T17:20:53Z")

</div>

On Monday, September 25, 2006, at 09:55AM, Rodrigo D.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

> **[BigBold - Informasi Tentang Bisnis dan Marketing](http://www.bigbold.com)**
>
> BigBold ialah situs yang memberikan segala macam Informasi Tentang Bisnis dan Marketing

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

---

<div class="post-metadata">

**Author:** ![rorra](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/df788c/32.png) [@rorra](https://www.ruby-forum.com/u/rorra)\
**Post date:** [September 25, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/cryptography-rsa/69681/6 "2006-09-25T16:54:52Z")

</div>

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