# AES block cipher artifacts

**URL:** <https://www.ruby-forum.com/t/aes-block-cipher-artifacts/191138>\
**Category:** Ruby\
**Created:** [July 6, 2010, 11:22am UTC](https://www.ruby-forum.com/t/aes-block-cipher-artifacts/191138 "2010-07-06T11:22:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 6, 2010, 11:22am UTC](https://www.ruby-forum.com/t/aes-block-cipher-artifacts/191138/1 "2010-07-06T11:22:59Z")

</div>

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](http://rubyforge.org/frs/download.php/30186/ruby-aes-optimized-1.1.gem))

Thanks,  
Jonathan G.

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 6, 2010, 12:05pm UTC](https://www.ruby-forum.com/t/aes-block-cipher-artifacts/191138/2 "2010-07-06T12:05:09Z")

</div>

Hi,

Am 06.07.2010 11:21, schrieb Jonathan G.:

> 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?

like you said, you use a BLOCK cipher. So the cipher will pad any data  
to match its block size. In your case the block size is 32 byte big (  
256 bit key size / 8 ).

> Here is a test program that shows the problem:
> 
> [snip]  
> 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  
> [snip]

You use encrypt\_BUFFER to encrypt, but decrypt\_BLOCK to decrypt. They  
are not symmetric. Aes.decrypt\_block is a lower level method than  
decrypt\_buffer. I don’t know the API, but I guess there should be a  
decrypt\_buffer, which removes the padding.

Waldemar Dick

---

<div class="post-metadata">

**Author:** ![jgeggatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jgeggatt](https://www.ruby-forum.com/u/jgeggatt)\
**Post date:** [July 6, 2010, 12:26pm UTC](https://www.ruby-forum.com/t/aes-block-cipher-artifacts/191138/3 "2010-07-06T12:26:13Z")

</div>

On Tue, Jul 06, 2010 at 07:04:58PM +0900, Waldemar Dick wrote:

> You use encrypt\_BUFFER to encrypt, but decrypt\_BLOCK to decrypt. They  
> are not symmetric. Aes.decrypt\_block is a lower level method than  
> decrypt\_buffer. I don’t know the API, but I guess there should be a  
> decrypt\_buffer, which removes the padding.

Thank you. Changing to descrypt\_buffer fixed things, and saved me from  
writing my own padding routine. Can’t explain why I did things that  
way earlier either, it was a few months ago 😉

Cheers,  
Jonathan
