Project

General

Profile

Bug #8720 ยป ecb_test.rb

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

 
#!/usr/bin/env ruby

require 'openssl'

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

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

print "Testing encryption: "

cipher.encrypt
ciphertext = cipher.update(PLAINTEXT) << cipher.final

if ciphertext == CIPHERTEXT
puts "OK!"
else
puts "FAILED! Got #{ciphertext.inspect} instead of #{CIPHERTEXT.inspect}"
end

print "Testing decryption: "

cipher.reset
cipher.decrypt
plaintext = cipher.update(CIPHERTEXT) << cipher.final

if plaintext == PLAINTEXT
puts "OK!"
else
puts "FAILED! Got #{plaintexttext.inspect} instead of #{PLAINTEXT.inspect}"
end
    (1-1/1)