Android AES Encryption Zero Padding

11,899

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());
}
Share:
11,899
Hank
Author by

Hank

Updated on June 04, 2022

Comments

  • Hank
    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
      Idistic over 12 years
      I understand the statement, not sure I understand your problem, are you asking how to construct a buffer with the correct padding or ?
    • Hank
      Hank over 12 years
      I 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
      Idistic over 12 years
      As 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
    Hank over 12 years
    What I'm specifically looking for is the name of the padding scheme, the one that I described above in my question.
  • timothyjc
    timothyjc over 12 years
  • Hank
    Hank over 12 years
    ANSI X.923 is similar but not the same because the length byte is also encrypted.
  • Chopstick
    Chopstick over 12 years
    I 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
    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
    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.