Project

General

Profile

Bug #8720 ยป ecb_test.rb

netjunki (Ben Lau), 08/02/2013 01:45 PM

 
1
#!/usr/bin/env ruby
2

    
3
require 'openssl'
4

    
5
# AES-128 ECB mode test vectors
6
# Taken from: http://www.inconteam.com/software-development/41-encryption/55-aes-test-vectors#aes-ecb-128
7
KEY        = ["2b7e151628aed2a6abf7158809cf4f3c"].pack("H*")
8
PLAINTEXT  = ["6bc1bee22e409f96e93d7e117393172a"].pack("H*")
9
CIPHERTEXT = ["3ad77bb40d7a3660a89ecaf32466ef97"].pack("H*")
10

    
11
cipher = OpenSSL::Cipher::Cipher.new("aes-128-ecb")
12
cipher.key = KEY
13
cipher.padding = 0 # Padding is enabled by default o_O
14

    
15
print "Testing encryption: "
16

    
17
cipher.encrypt
18
ciphertext = cipher.update(PLAINTEXT) << cipher.final
19

    
20
if ciphertext == CIPHERTEXT
21
  puts "OK!"
22
else
23
  puts "FAILED! Got #{ciphertext.inspect} instead of #{CIPHERTEXT.inspect}"
24
end
25

    
26
print "Testing decryption: "
27

    
28
cipher.reset
29
cipher.decrypt
30
plaintext = cipher.update(CIPHERTEXT) << cipher.final
31

    
32
if plaintext == PLAINTEXT
33
  puts "OK!"
34
else
35
  puts "FAILED! Got #{plaintexttext.inspect} instead of #{PLAINTEXT.inspect}"
36
end