Skip to Main Content

Java Security

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

CBC-MAC-AES in Java

843811Apr 22 2010 — edited Apr 22 2010
Hi everyone !

I have a C++ source code that uses AES with CBC mode to create MACs. I would like to do the same in Java (no special padding as it is manually padded with zero bytes), but I can't make it work after hours :-(

I've tried using BoucyCastle libraries with this kind of code :

(key is a 16-length byte array, and msg is a byte array padded with zeros (length is multiple of 16 bytes))
BlockCipher cipher = new AESLightEngine();
CBCBlockCipherMac mac = new CBCBlockCipherMac(cipher);

// use the key to create cipherparameters
KeyParameter keyparam = new KeyParameter(key);

// init cipher
mac.init(keyparam);

byte[] result = new byte[mac.getMacSize() + 8];

// read bytes into buffer and feed these bytes into Hmac object
mac.update(msg, 0, length);	
mac.doFinal(result, mac.getMacSize());

return result;
but it does not return the MAC that my C++ code returns (with the same pair of msg/key). Note : The MAC the c++ returns is OK (I'm sure of it).

Could someone help me ?
Thx a lot in advance.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 20 2010
Added on Apr 22 2010
5 comments
1,987 views