Search code examples
javastringencryptionaes

How to decrypt encrypted string using AES Algorithm in Java?


I can't decrypt the AES encrypted text in Java. Here's my code:

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.xml.bind.DatatypeConverter;

public class aesencrypt {

    public static void main(String[] args) throws Exception {
        String plainText = "Hello World";
        System.out.println("Original Text:" + plainText);
        SecretKey secKey = getSecretEncryptionKey();
        System.out.println("AES Key (Hex Form):"+bytesToHex(secKey.getEncoded()));
        String encryptedText = bytesToHex(encryptText(plainText, secKey));
        System.out.println("Encrypted Text (Hex Form):"+encryptedText);
        String decryptedText = decryptText(encryptedText, secKey);
        System.out.println("Descrypted Text:"+decryptedText);
    }

    public static SecretKey getSecretEncryptionKey() throws Exception{
        KeyGenerator generator = KeyGenerator.getInstance("AES");
        generator.init(128); // The AES key size in number of bits
        SecretKey secKey = generator.generateKey();
        return secKey;
    }

    public static byte[] encryptText(String plainText,SecretKey secKey) throws Exception{
        // AES defaults to AES/ECB/PKCS5Padding in Java 7
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
        byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes());
        return byteCipherText;
    }

    public static String decryptText(String encrypted, SecretKey secKey) throws Exception {
        // AES defaults to AES/ECB/PKCS5Padding in Java 7
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.DECRYPT_MODE, secKey);
        byte[] bytePlainText = aesCipher.doFinal(encrypted.getBytes());
        return new String(bytePlainText);
    }

    private static String bytesToHex(byte[] hash) {
        return DatatypeConverter.printHexBinary(hash);
    }

}

When I run this, I'm getting error in the following output:

Original Text:Hello World
AES Key (Hex Form):4690FFCDC7B5E8B128F5BF45F0920527
Encrypted Text (Hex Form):7C092F40D592F9DF83F3D4E976612928
Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:989)
    at com.sun.crypto.provider.CipherCore.doFinal(CipherCore.java:845)
    at com.sun.crypto.provider.AESCipher.engineDoFinal(AESCipher.java:446)
    at javax.crypto.Cipher.doFinal(Cipher.java:2165)
    at aesencrypt.decryptText(aesencrypt.java:38)
    at aesencrypt.main(aesencrypt.java:15)

I've already searched Stack Overflow and search engines but I can't actually find the solution.

PROBLEM SOLVED

UPDATE: Problem Solved, thanks to JB Nizet for the solution!

Revised Code :

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.xml.bind.DatatypeConverter;

public class aesencrypt {

    public static void main(String[] args) throws Exception {
        String plainText = "Hello World";
        System.out.println("Original Text:" + plainText);
        SecretKey secKey = getSecretEncryptionKey();
        System.out.println("AES Key (Hex Form):"+bytesToHex(secKey.getEncoded()));
        String encryptedText = bytesToHex(encryptText(plainText, secKey));
        System.out.println("Encrypted Text (Hex Form):"+encryptedText);
        String decryptedText = decryptText(encryptedText, secKey);
        System.out.println("Decrypted Text:"+decryptedText);
    }

    public static SecretKey getSecretEncryptionKey() throws Exception{
        KeyGenerator generator = KeyGenerator.getInstance("AES");
        generator.init(128);
        SecretKey secKey = generator.generateKey();
        return secKey;
    }

    public static byte[] encryptText(String plainText,SecretKey secKey) throws Exception{
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.ENCRYPT_MODE, secKey);
        byte[] byteCipherText = aesCipher.doFinal(plainText.getBytes());
        return byteCipherText;
    }

    public static String decryptText(String encrypted, SecretKey secKey) throws Exception {
        Cipher aesCipher = Cipher.getInstance("AES");
        aesCipher.init(Cipher.DECRYPT_MODE, secKey);
        byte[] bytePlainText = aesCipher.doFinal(hexToByte(encrypted));
        return new String(bytePlainText);
    }

    private static String bytesToHex(byte[] hash) {
        return DatatypeConverter.printHexBinary(hash);
    }

    private static byte[] hexToByte(String txt) {
        return DatatypeConverter.parseHexBinary(txt);
    }

}

Here's the output:

Original Text:Hello World
AES Key (Hex Form):84526F32BEDDBEA5BFBCDE241AD9BBA2
Encrypted Text (Hex Form):25378032E5F52575B7CEF311D45F00BD
Decrypted Text:Hello World

Solution

  • Look at what you're doing:

    String encryptedText = bytesToHex(encryptText(plainText, secKey));
    

    Here you transform the plain text into bytes (using the lossy, platform-dependant getBytes(), BTW. You should use UTF8).

    Then you encrypt those bytes and get a byte array as a result. Then you encode this byte array to hexadecimal.

    So, the reverse operation would be: decode from hexadecimal to bytes, then decrypt those bytes, then construct a String from the descrypted bytes.

    But here's what you're doing:

    String decryptedText = decryptText(encryptedText, secKey);
    

    where decryptText() does:

    byte[] bytePlainText = aesCipher.doFinal(encrypted.getBytes());
    

    So, you're never using the hexadecimal decoding. If you use bytesToHex() on one side, you need to use hexToBytes() on the other side.