From: Jonathan Groll on
I've got an oddity with the AES block cipher. There are (seemingly
random) artifact bytes that block fill a block passed through the
encryption algorith. For instance, starting with "23" this gets
transformed to
"23\016\016\016\016\016\016\016\016\016\016\016\016\016\016"

The question is, how to reversibly perform the cipher without the
artifacts?

Here is a test program that shows the problem:

#!/usr/bin/env ruby

require 'rubygems'
require 'ruby-aes'

class CipherTest

KEY_LENGTH = 256.freeze
MODE = 'CBC'.freeze

def initialize(block_secret, iv_secret)
@block_secret = block_secret
@iv_secret = iv_secret
end

def aes_encrypt(value)
return Aes.encrypt_buffer(KEY_LENGTH, MODE, @block_secret,
@iv_secret, value)
end

def aes_decrypt(v)
Aes.decrypt_block(KEY_LENGTH, MODE, @block_secret, @iv_secret, v)
end
end

if __FILE__ == $0
seeker = CipherTest.new("djkfljkljfakljgfakfkajflakjfdfhs",
"0987654321ABCDEF01234567890ABCDE")
testval = "23"
encrypted = seeker.aes_encrypt(testval)
p encrypted
decrypted = seeker.aes_decrypt(encrypted)
p decrypted
if decrypted == testval
puts 'they match, encryption is reversible'
else
puts 'they differ!'
end
end

ruby-aes is the optimized version from rubyforge
(http://rubyforge.org/frs/download.php/30186/ruby-aes-optimized-1.1.gem)

Thanks,
Jonathan Groll