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());
}
  • You're saving the ciphertext into 'encryptText', but then you're passing 'text1' to the decryption function. Where is 'text1' coming from? – Sean Burton Oct 17 '13 at 10:08

1 Answers1

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");
  • 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" –  Oct 10 '13 at 16:53
  • 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. –  Oct 10 '13 at 16:55
  • 1
    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. –  Oct 10 '13 at 17:38
  • 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. –  Oct 10 '13 at 18:01
  • 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.. –  Oct 10 '13 at 18:02
  • You know what.. I was designating the parameters wrong. I switched it to AES/CBC/NoPadding. And made a parameter to pass into Cipher that was just a 16 byte key. And it all seems to work! Thank you again for your help. –  Oct 10 '13 at 19:38
  • No problem. I'm glad it worked out for you. You may find the answer to this question helpful as well in your research: http://stackoverflow.com/questions/10935068/what-are-the-cipher-padding-strings-in-java –  Oct 10 '13 at 20:24
  • @Bob: Just so you know, the fact that you _can_ get that error means your system is vulnerable to [padding oracle attacks](http://en.wikipedia.org/wiki/Padding_oracle). Those might or might not be relevant in your use case, but it sure doesn't inspire confidence. You might want to read up a bit on [block cipher modes of operation](http://en.wikipedia.org/wiki/Cipher_mode) so you'll know what you're really doing. (Also look up [authenticated encryption](http://en.wikipedia.org/wiki/Authenticated_encryption), which is what you really [should be using](http://crypto.stackexchange.com/q/12178).) – Ilmari Karonen Dec 11 '13 at 04:33