[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: cryptography (RSA

Gavin Kistner

9/22/2006 9:12:00 PM

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

1 Answer

Rodrigo Dominguez

9/23/2006 4:07:00 PM

0

Gavin Kistner 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

--
Posted via http://www.ruby-....