From: Rob Biedenharn Date: 2009-03-24T12:43:39+09:00 Subject: Re: Mimic AES_ENCRYPT and AES_DECRYPT functions in Ruby On Mar 23, 2009, at 11:25 PM, Felipe Coury wrote: > Because AES is a block-level algorithm, padding is used to encode > uneven > length strings and so the result string length may be calculated using > this formula: > > 16 × (trunc(string_length / 16) + 1) Do you mean to have: 16 * (string_length + 15)/16 If the string length is 32, what do you expect the result to be? Your formula gives 48, (16*(trunc(32/16)+1))==(16*(2+1)), while mine gives 32, (16*(32+15)/16)==(16*(47/16))==(16*2) [integer division]. Here's a bit of code that I've lifted out of another project: # Encrypt the content of the document, block by block, in a manner # compatible with the original Python (so we can decrypt it and # remain backwardly compatible) rijndael = Crypt::Rijndael.new(self.key, 256, 256) encryptedData = "" data << 'X' # a marker added to cope with partial block blocks, bytes = data.length.divmod(32) unless bytes.zero? data << "\0" * (32 - bytes) blocks += 1 end (0...blocks).each do |block| encryptedData << rijndael.encrypt_block(data[block * 32, 32]) end The decrypting side was Java and I don't know why the 'X' was chosen (seems that I recall something about there being a byte with the number of extra bytes of padding... or your formula might hold a clue). Anyway, you'd have to adjust it for 128-bit/16-byte keys (and blocks). -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com