Rinjdael (AES) encryption in Ruby

F

fightinjoe

I've got a quick general question about encryption.

I've encrypted the same string with OpenSSL, ruby-aes, and Crypt, using
the same cipher, key, and IV (I've slightly modified Crypt to accept an
IV) and get different results for all of them.

If all of the variables are the same, why would this be happening?

---- Begin Code Snippit (http://pastie.caboo.se/25895)

require 'crypt/rijndael'
require 'base64'
require 'ruby-aes/aes'
require 'openssl'

# Modify Crypt::Rijndael lib to accept an IV
module Crypt
class Rijndael
def vector() @vector; end

alias orig_initialize initialize
def initialize(key, vector, keyBits = 256, blockBits = 128)
@vector = vector
orig_initialize(key, keyBits, blockBits)
end

alias orig_generate_initialization_vector
generate_initialization_vector
def generate_initialization_vector( *p )
@vector || orig_generate_initialization_vector( *p )
end

def iv_length() block_size; end

end
end

def test_crypt_encryption
key = '0123456789abcdef0123456789abcdef'
rijndael = Crypt::Rijndael.new( key, 128, 128 )

raise Base64.encode64( rijndael.encrypt_string( 'abcdef' ) )
# returns 'F2wpWlldcZPQlfHOBWZatk8Zq9XNHMRJiB0vC04rZEE='
end

def test_openssl_encryption
key = '0123456789abcdef0123456789abcdef'
alg = "AES-256-CBC"
iv = 'abcdefghijklmnop'

aes = OpenSSL::Cipher::Cipher.new(alg)
aes.encrypt
aes.key = key
aes.iv = iv

out = aes.update('abcdef')
out << aes.final
raise Base64.encode64(out)
# returns 'jQeE5X55BSwJQav1VV+w1g=='
end

def test_aes_encryption
key = '0123456789abcdef0123456789abcdef'
iv = 'abcdefghijklmnop'
raise Base64.encode64( Aes.encrypt_buffer(128, 'CBC', key, iv,
'abcdef') )
# returns 'fa3taon6wtaPGQ4KgmLrCQ=='
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top