Android AES Encryption Zero Padding
Not sure if you already solved your problem, but I think the padding type you're looking for is ZeroBytePadding. This code works for me on the 2.3.3 simulator:
String stringKey = "60380131061660211660380426804995";
String message = "This is a secret message";
try {
SecretKeySpec sks = new SecretKeySpec(stringKey.getBytes(),"AES");
Cipher c = Cipher.getInstance("AES/ECB/ZeroBytePadding"); // Change to CBC and use appropriate IV
c.init(Cipher.ENCRYPT_MODE, sks);
c.update(message.getBytes());
byte[] ciphertext = c.doFinal();
Log.i("CE", new String(ciphertext));
} catch (NoSuchAlgorithmException e) {
Log.e("CE",e.getMessage());
} catch (NoSuchPaddingException e) {
Log.e("CE",e.getMessage());
} catch (InvalidKeyException e) {
Log.e("CE",e.getMessage());
} catch (IllegalBlockSizeException e) {
Log.e("CE",e.getMessage());
} catch (BadPaddingException e) {
Log.e("CE",e.getMessage());
}
Hank
Updated on June 04, 2022Comments
-
Hank almost 2 years
I need to encrypt a file using AES, with a CBC block size of 16 bytes, and key of 256 bits. And the file needs to be zero padded to a multiple of 16 bytes. And after encrypting the file the amount of padded zeros needs to be appended to the end.
For example, if I have a file that is 9 bytes, 7 zero bytes will be appended. The 16 bytes will be encrypted and then the length of the read data, 9, will be appended to the end. So the total length should be 17 bytes with the last byte unencrypted.
I'm using the Cipher class, and I know to use "AES/CBC/" but I don't know if there is a padding method that describes what I want.
EDIT: The last byte should be how many bytes are real data in the last 16 byte padded cell.
-
Idistic over 12 yearsI understand the statement, not sure I understand your problem, are you asking how to construct a buffer with the correct padding or ?
-
Hank over 12 yearsI just want to know if there is a name for the padding method or do I have to do this manually? And if there is a name, is it supported?
-
Idistic over 12 yearsAs stated in timothyjc's answer it has padding mechanisms available so that padding is taken care of for you "AES/ECB/PKCS7Padding" takes care of block sizes up to 255 bytes
-
-
Hank over 12 yearsWhat I'm specifically looking for is the name of the padding scheme, the one that I described above in my question.
-
timothyjc over 12 yearsYou can try ANSI X.923... stackoverflow.com/questions/1329730/java-ansi-x923-padding, en.wikipedia.org/wiki/Padding_%28cryptography%29.
-
Hank over 12 yearsANSI X.923 is similar but not the same because the length byte is also encrypted.
-
Chopstick over 12 yearsI re-read your question and I think ZeroBytePadding is not what you want. If ANSI X.923 is also not what you want (because you don't want the last byte encrypted), then you will have to write the padding routine yourself. So pad the message with X.923, copy out the last byte, encrypt and then append the last byte with the zero-pad length to the file.
-
One Two Three almost 12 years
Cipher cipher = Cipher.getInstance("AES/ECB/ZeroBytePadding");
yields'Cannot Find any provider supporting "AES/ECB/ZeroBytePadding"
. Do you happen to know what provider I should use? -
Maarten Bodewes over 10 years@OneTwoThree ZeroBytePadding should work in Android as it is based upon the Bouncy Castle code, but not in the JRE. If it is not available, add and configure the Bouncy Castle provider or use Spongy Castle.