0

I have a question about using Decrypt in AES. I wrote the same program that encrypts the text.

Here is my Decrypt class. (I use a 16 byte key).

public static byte[] decryptAES(String message) throws Exception 
{  
String secretKey = "JohnIsAwesome!1!";
SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
return cipher.doFinal(message.getBytes());  
}

Here is my main. The encrypt is working perfectly.

public static void main (String[] args) throws Exception
{
    String text = "MySuperSecretPassword!";
    //Ecrypt the Text, then print it out in an array
    String encryptText = Arrays.toString(encryptAES(text));
    System.out.println("Encrypted Message"+ encryptText);

    //Decrypt the Text, then print it out in an array
    String decryptText = Arrays.toString(decryptAES(text1));
    System.out.println("Decrypted Message"+ decryptText);

}

Encrypt output:

Encrypted Message[16, 69, 84, 118, 68, -36, -67, 125, -86, -106, -4, 24, -59, -77, -41, -32, -37, 104, -44, -42, 112, 87, 87, 101, 28, 99, 60, -27, 34, -88, -17, -114]

If anyone has any ideas why the decryption would not work, It would be greatly appreciated. I've been banging my head against the wall on this one.

Thank you

Sorry, forgot to add my Encrypt class here as well.

public static byte[] encryptAES(String message) throws Exception
{
    String secretKey = "JohnIsAwesome!1!";
    SecretKeySpec key = new SecretKeySpec(secretKey.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, key);
    return cipher.doFinal(message.getBytes());
}
1
  • You're saving the ciphertext into 'encryptText', but then you're passing 'text1' to the decryption function. Where is 'text1' coming from? Commented Oct 17, 2013 at 10:08

1 Answer 1

1

Arrays.toString(byte[] a) "Returns a string representation of the contents of the specified array." It does not convert a byte array to a String. Instead try using:

new String(decryptAES(text1), "UTF-8");
Sign up to request clarification or add additional context in comments.

8 Comments

Alright, I used your suggestion and it works perfectly for the encryption. But the decryption is still having issues. I just got this "Input length must be multiple of 16 when decrypting with padded cipher"
It doesn't make sense to me that my 16 byte key for encryption is the exact same 16 byte key for decryption. I'm not sure why I get that error just for the decrypt and not the encrypt.
That is a different issue. It isn't related to your key, but related to the length of the data. It likely has to be 16 bit block sizes for the default AES padding scheme. Try instead specifying an exact transformation when creating the cipher instance, so use instead Cipher.getInstance("AES/CBC/PKCS5Padding") in both decryptAES and encryptAES. See if that makes a difference.
Huh, interesting. I just switched the decryptAES and encryptAES to those parameters, Now I'm getting this error. "Exception in thread "main" java.security.NoSuchProviderException: No such provider: AES" Not sure, haven't seen this error before.
well, I take that back now.. I was able to fix that. Now getting this error instead. "Exception in thread "main" javax.crypto.BadPaddingException: Given final block not properly padded" The encryption is still working fine however..
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.