2

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
v_ag
  • 278
  • 1
  • 5
  • 17
  • that exception means your secret key used to decrypt is wrong – Jeryl Cook Jan 19 '18 at 15:45
  • use https://docs.spring.io/spring-security/site/apidocs/org/springframework/security/util/EncryptionUtils.html and be done with it. :) – Jeryl Cook Jan 19 '18 at 15:47
  • @JerylCook You can clearly see that the secret key variable - secKey used in decryption is exactly the same as used in encryption. How can be the secret key wrong then? – v_ag Jan 19 '18 at 15:49
  • 1
    look at the answer below..its not the same due to how your decoding the key...that's why you get the padding error. – Jeryl Cook Jan 19 '18 at 15:57

1 Answers1

3

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Problem has been solved but still I want to know how can I convert hexadecimal secret key into original secret key. For example : `// this stores secret key in hex form` `String hexSecKey = bytesToHex(getSecretEncryptionKey().getEncoded());` `// how to get original key from hex secret Key?` `SecretKey secKey = ???` – v_ag Jan 20 '18 at 04:22
  • Got the answer from here: https://stackoverflow.com/questions/5355466/converting-secret-key-into-a-string-and-vice-versa – v_ag Jan 20 '18 at 05:03