0
votes

So I'm having issues decrypting a file or a string using java AES encryption. So I have a text variable containing binary numbers. I created a random key. Then I encrypted the text and returned the encrypted byte[] and wrote this into a .txt file. And the encryption process works.

Then I grabbed all the bytes from exampleOrig.txt and did the decryption process and returned an error below. I'm not sure what's wrong and doing some research, it didn't really help. Any help would be appreciated. Thanks!

public static void main(String[] args) throws Exception {
    // Generate AES Key
    KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
    SecretKey myAesKey = keyGenerator.generateKey();

    Cipher aesCipher = Cipher.getInstance("AES");

    String text = "11111110001100110011011111111011011111111101000111000101111111111111111001011110110001011111110111111001110110011100110111011111101111100111101";

    // ENCRYPT the text
    aesCipher.init(Cipher.ENCRYPT_MODE, myAesKey);
    byte[] textEncrypted = aesCipher.doFinal(text.getBytes());

    // Output results
    System.out.println("Text [Byte Format]: " + text);
    System.out.println("Text : " + new String(text));
    System.out.println("Text Encrypted: " + textEncrypted);

    // Write the 'text' to a file
    File encryptFileResult = new File("TestFiles/exampleOrig.txt");
    if (!encryptFileResult.exists()) {
        encryptFileResult.createNewFile();
    } else {
        encryptFileResult.delete();
        encryptFileResult.createNewFile();
    }

    FileWriter encryptFileWriter = new FileWriter(encryptFileResult.getAbsoluteFile());
    BufferedWriter bufferedWriter = new BufferedWriter(encryptFileWriter);

    bufferedWriter.write(new String(textEncrypted));
    bufferedWriter.close();

    // Grab all bytes from the 'exampleOrig.txt' file
    byte[] encryptedBytes = Files.readAllBytes(encryptFileResult.toPath());

    // DECRYPT the text
    aesCipher.init(Cipher.DECRYPT_MODE, myAesKey);
    byte[] textDecrypted = aesCipher.doFinal(encryptedBytes);

    System.out.println("Text Decrypted: " + new String(textDecrypted));
}

ERROR MESSAGE:

Exception in thread "main" javax.crypto.IllegalBlockSizeException: Input length must be multiple of 16 when decrypting with padded cipher
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:913)
at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:824)
at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:436)
at javax.crypto.Cipher.doFinal(Cipher.java:2165)
at main.AESEncryption.main(AESEncryption.java:50)
1
Let me try it outuser3851283
No, it didn't work. I get the same error message as above.user3851283

1 Answers

1
votes
System.out.println("Text : " + new String(text));
System.out.println("Text Encrypted: " + textEncrypted);

Your code above is meaningless for encrypted data, as encrypted data is binary. You should instead loop over the byte array and print each byte out by converting them to HEX:

for (byte b : textEncrypted) {
  if ((b & 0xFF) < 0x10) {
    System.out.print("0");
  }
  System.out.print(Integer.toHexString(b & 0xFF).toUpperCase() + " ");
}
System.out.println();

Your error is because of FileWriter. FileWriter is for writing character data, you should use a FileOutputStream instead.

Compare the byte array textEncrypted with encryptedBytes and you will see.