From: Roland Schmitt Date: 2006-09-25T16:43:04+09:00 Subject: Re: cryptography (RSA) Rodrigo Dominguez schrieb: > 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: > ----------------------------------------------- > > $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 > ----------------------------------------------- > > $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? > > > 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