Re: cryptography (RSA)

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.

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs