Rinjdael (AES) encryption in Ruby

Discussion in 'Ruby' started by fightinjoe@gmail.com, Dec 5, 2006.

  1. Guest

    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
     
    , Dec 5, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lars J.
    Replies:
    0
    Views:
    480
    Lars J.
    Jul 31, 2005
  2. Chris Curvey

    modes for AES encryption?

    Chris Curvey, Mar 24, 2005, in forum: Python
    Replies:
    1
    Views:
    374
    Chris Curvey
    Mar 29, 2005
  3. Tuvas

    AES encryption

    Tuvas, Mar 7, 2006, in forum: Python
    Replies:
    5
    Views:
    413
    Bryan Olson
    Mar 11, 2006
  4. Anth
    Replies:
    6
    Views:
    234
    Brian Candler
    Sep 2, 2009
  5. Nate Wiger

    New AES gem available -- fast-aes

    Nate Wiger, Jan 20, 2010, in forum: Ruby
    Replies:
    3
    Views:
    198
    Pål Bergström
    Jul 1, 2010
Loading...

Share This Page