Skip to Main Content

Java Programming

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!

AES encryption with Java

807588Jan 29 2009 — edited Feb 1 2009
Hello Everyone,

I'm stumped. I'm pretty sure that if you encrypt plaintext that is 16
bytes long you should receive ciphertext that is 16 bytes long.
However, I'm encrypting 16 byte plaintext and ending up with 32 byte
ciphertext. If I remove one character from the plaintext so it is 15
bytes long I end up with a 16 byte ciphertext.

Here is some pseudo-code that I'm using:

KeyGenerator generator = KeyGenerator.getInstance("AES");
generator.init(128);
key = generator.generateKey();
cipher = Cipher.getInstance("AES");

cipher.init(Cipher.ENCRYPT_MODE, key);
encrypted_data = cipher.doFinal(inputBytes);

I've written out the following information:

SunJCE Provider (implements RSA, DES, Triple DES, AES, Blowfish,
ARCFOUR, RC2, PBE, Diffie-Hellman, HMAC)
Input Length: 16
Key Length: 16
Cipher Block Size: 16
Cipher Algorithm: AES
Predicted Output size: 32
Encrypted Length: 32
Encoded Key: PcMq9kTP82cCHygb8fQllQ==
Encoded Data:u57GmLFHozZdyPFFILVQpRIOB2ETUrHbqX2OXTG9qa4=
Decrypted: 1234567890123456

Another thing that has me stumped is that I can't figure out what the
default padding and what the default mode of operation is
(ECB,CBC,CTR,etc.) I suspect it is using PKCS5Padding because the 32
bytes in my example are filled with the ascii character 16, but I
can't find a method that will tell me that. Does anyone happen to
know if one exists.

Any help deciphering this would really be appreciated.

Thanks.

JavaRanger.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 1 2009
Added on Jan 29 2009
6 comments
1,449 views