Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Mar 8th, 2011  |  syntax: None  |  size: 1.47 KB  |  views: 117  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require 'crypt/rijndael'
  2. require 'base64'
  3. require 'ruby-aes/aes'
  4. require 'openssl'
  5.  
  6. # Modify Crypt::Rijndael lib to accept an IV
  7. module Crypt
  8.   class Rijndael
  9.     def vector() @vector; end
  10.  
  11.     alias orig_initialize initialize
  12.     def initialize(key, vector, keyBits = 256, blockBits = 128)
  13.       @vector = vector
  14.       orig_initialize(key, keyBits, blockBits)
  15.     end
  16.  
  17.     alias orig_generate_initialization_vector generate_initialization_vector
  18.     def generate_initialization_vector( *p )
  19.       @vector || orig_generate_initialization_vector( *p )
  20.     end
  21.  
  22.     def iv_length() block_size; end
  23.  
  24.   end
  25. end
  26.  
  27.   def test_crypt_encryption
  28.     key = '0123456789abcdef0123456789abcdef'
  29.     rijndael = Crypt::Rijndael.new( key, 128, 128 )
  30.  
  31.     raise Base64.encode64( rijndael.encrypt_string( 'abcdef' ) )
  32.     # returns 'F2wpWlldcZPQlfHOBWZatk8Zq9XNHMRJiB0vC04rZEE='
  33.   end
  34.  
  35.   def test_openssl_encryption
  36.     key = '0123456789abcdef0123456789abcdef'
  37.     alg = "AES-256-CBC"
  38.     iv = 'abcdefghijklmnop'
  39.  
  40.     aes = OpenSSL::Cipher::Cipher.new(alg)
  41.     aes.encrypt
  42.     aes.key = key
  43.     aes.iv = iv
  44.  
  45.     out = aes.update('abcdef')
  46.     out << aes.final
  47.     raise Base64.encode64(out)
  48.     # returns 'jQeE5X55BSwJQav1VV w1g=='
  49.   end
  50.  
  51.   def test_aes_encryption
  52.     key = '0123456789abcdef0123456789abcdef'
  53.     iv  = 'abcdefghijklmnop'
  54.     raise Base64.encode64( Aes.encrypt_buffer(128, 'CBC', key, iv, 'abcdef') )
  55.     # returns 'fa3taon6wtaPGQ4KgmLrCQ=='
  56.   end
clone this paste RAW Paste Data