Liam Potter
Liam Potter

Reputation: 1824

AES Encryption Given Final Block Not Properly Padded

I'm trying to create a class that will allow me to encrypt and decrypt strings using the AES algorithm. I'm using the exception from http://aesencryption.net/#Java-aes-encryption-example but have modified the code to suit my needs.

This is my Main.java:

public class Main {

    public static void main(String[] args) {

        AES256 aes = new AES256();

        aes.setKey("Secret Key");

        String enc = "";
        enc = aes.encrypt("qwertyuiopasdfgh");

        System.out.println(enc);
        System.out.println(aes.decrypt(enc));


    }

}

And this is my AES256.java:

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

public class AES256 {

    private SecretKeySpec secretKey;
    private byte[] key;

    public void setKey(String key) {    
        MessageDigest sha = null;
        try {
            this.key = key.getBytes("UTF-8");
            sha = MessageDigest.getInstance("SHA-1");
            this.key = sha.digest(this.key);
            this.key = Arrays.copyOf(this.key, 16);
            secretKey = new SecretKeySpec(this.key, "AES");
        } catch (UnsupportedEncodingException | NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    public String getSecretKey() {
        return secretKey.toString();
    }

    public String getKey() {
        return new String(key);
    }

    public String encrypt(String string) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            return Base64.getMimeEncoder().encodeToString(string.getBytes());
        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

    public String decrypt(String string) {
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING");
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            return new String(cipher.doFinal(Base64.getMimeDecoder().decode(string.getBytes())));
        } catch (InvalidKeyException | NoSuchAlgorithmException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException e) {
            e.printStackTrace();
        }
        return null;
    }

}

This is the error that is being thrown:

javax.crypto.BadPaddingException: Given final block not properly padded
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:966)
    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:2121)
    at AES256.decrypt(AES256.java:55)
    at Main.main(Main.java:13)

Does anybody know what is causing this error?

Upvotes: 1

Views: 9424

Answers (1)

dst
dst

Reputation: 3337

You return the original string in its base64-encoded form:

return Base64.getMimeEncoder().encodeToString(string.getBytes());

You'd want to use the cipher in there as well:

return Base64.getMimeEncoder().encodeToString(cipher.doFinal(string.getBytes()));

Independent of that, when depolying own crypto please be aware of the impacts of cipher modes, padding, etc. For example the ECB mode you're using will produce the same ciphertext from the same plaintext, e.g. the ciphertext might lead hints about the original text, as in the famous encrypted tux image:

enter image description here

Image Copyright: All uses are permitted provided that Larry Ewing, the owner of the original image, who requires that you mention him, his email address, [email protected], and The GIMP, according to http://www.isc.tamu.edu/~lewing/linux/.

For more details on that, see Wikipedia's article about block cipher modes.

Upvotes: 3

Related Questions