MadCrypt Padding Query

delphi package - madRes, madTools, madStrings, ...
Post Reply
paysys
Posts: 2
Joined: Tue Feb 08, 2005 11:50 pm

MadCrypt Padding Query

Post by paysys »

I have done some testing on madCrypt and notice that if the data that I want to encrypt is an exact multiple of 8 characters then it adds another 8 padding characters before encrypting.
The lines that cause this are the 4th & 5th lines in the Encrypt procedure:
i2 := 8 - (i1 and 7)
SetLength(data, i1 + i2)

Is this intentional?

If I replace the 4th line with the following lines:
i2 := 0;
if i1 mod 8 > 0 then
i2 := i1 mod 8;
it then only adds padding if the data is not a multiple of 8 characters.

Is this correct or is there something I am missing?
madshi
Site Admin
Posts: 10754
Joined: Sun Mar 21, 2004 5:25 pm

Post by madshi »

It is intentional. That's how padding is meant to work. If I'd do it differently, it wouldn't be compatible with other encryption/decryption packages.

Look: If the decryptor gets a chunk of 8 bytes, how is it supposed to know whether it was 8 bytes in reality or maybe 5? By always using padding (even if the length is already "even") the decryptor doesn't need to guess whether padding bytes are appended or not. It just knows that there are padding bytes, no matter what.
paysys
Posts: 2
Joined: Tue Feb 08, 2005 11:50 pm

Post by paysys »

Thanks for explaining that.

I got a little confused because I was encrypting some data in PHP and then encrypting the same data in Delphi. The results weren't exactly the same.

Anyway, as a test I encrypted again in PHP, took the results and tried decrypting in Delphi. It worked except for some padding at the end decrypted of the decrypted data, which I can simply strip off. I'll do some more testing to make sure it works the way I think it will
Post Reply