AES/CTR/NoPadding and ciphertext bytes

J

jimgardener

hi
I encoded a byte array using AES/CTR/NoPadding scheme using a key
created with KeyGenerator.However when i checked the ciphertext byte
array I found that it is longer than the plaintext bytes.A block of
zero bytes seem to have been appended to it

this is how it went
Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");
generator.init(192);
Key encryptionKey = generator.generateKey();
cipher.init(Cipher.ENCRYPT_MODE, encryptionKey, new IvParameterSpec
(ivBytes));
byte[] cipherText = new byte[cipher.getOutputSize(input.length)];

System.out.println("input : " + Utils.toHex(input)
+"\nbytes="+input.length);
System.out.println("cipherText : " + Utils.toHex(cipherText) +
"\nbytes: " + cipherText.length);

--------------------------------------
these are the hex of the plaintext and ciphertext bytes>>

input :
000102030405060708090a0b0c0d0e0f0001020304050607
bytes=24
cipherText :
263f5032cf8346031e77dce0eb1d91b7c3c9b9ca17e52d8b0000000000000000
bytes=32

should there be this additional block even when NoPadding is
mentioned?

thanks
jim
 
J

jimgardener

This is not the output from the code you posted because you never
actually encrypt your input into the cipherText array.  As posted the cipherText array is all zeros.


thanks rossum,
missed some lines of code while doing copy-paste..below is the code i
used to get the previous output.


byte[] input = new byte[] {
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 };
byte[] ivBytes = new byte[] {
0x00, 0x00, 0x00, 0x01, 0x04, 0x05, 0x06, 0x07,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };

Cipher cipher = Cipher.getInstance("AES/CTR/NoPadding", "BC");
KeyGenerator generator = KeyGenerator.getInstance("AES", "BC");
generator.init(192);
Key encryptionKey = generator.generateKey();

System.out.println("input :\n" + Utils.toHex(input)
+"\nbytes="+input.length);

cipher.init(Cipher.ENCRYPT_MODE, encryptionKey,new IvParameterSpec
(ivBytes));

byte[] cipherText = new byte[cipher.getOutputSize(input.length)];
int ctLength = cipher.update(input, 0, input.length, cipherText, 0);
ctLength += cipher.doFinal(cipherText, ctLength);
System.out.println("cipherText :\n" + Utils.toHex(cipherText)
+"\nbytes="+cipherText.length);


thanks for the detailed reply..it makes things a lot clearer..
jim
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top