# Re: cryptography (RSA)

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

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 22, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/re-cryptography-rsa/69682/1 "2006-09-22T23:14:31Z")

</div>

> ## puts %(encrypted text: #{cipher.inspect})
> 
> ## 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?

irb(main):001:0\> 140.chr  
=\> “\214”  
irb(main):002:0\> puts 140.chr  
î  
irb(main):003:0\> 140.to\_s( 8 )  
=\> “214”

The .inspect version of a string escapes non-ascii characters. \214  
means “the character with the byte value of octal 214, decimal 140”.

\t is a tab character.

Remove the .inspect from your above and you’ll see something more like  
the python values you seem to have been expecting.

---

<div class="post-metadata">

**Author:** ![phrogz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/2acd7d/32.png) [@phrogz](https://www.ruby-forum.com/u/phrogz)\
**Post date:** [September 23, 2006, 6:07pm UTC](https://www.ruby-forum.com/t/re-cryptography-rsa/69682/2 "2006-09-23T18:07:08Z")

</div>

Gavin K. wrote:

> > ## puts %(encrypted text: #{cipher.inspect})
> > 
> > ## 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?
> 
> irb(main):001:0\> 140.chr  
> =\> “\214”  
> irb(main):002:0\> puts 140.chr  
> ï¿½  
> irb(main):003:0\> 140.to\_s( 8 )  
> =\> “214”
> 
> The .inspect version of a string escapes non-ascii characters. \214  
> means “the character with the byte value of octal 214, decimal 140”.
> 
> \t is a tab character.
> 
> Remove the .inspect from your above and you’ll see something more like  
> the python values you seem to have been expecting.  
> thank you, but I’m having a lot of problems anyway

This is the new code:

## 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
