Can anyone see anything that's obviously wrong with my decrypt method in the code below? I'm not getting any errors during any part of the code, it's just that the decrypted data is coming out as random garbage.
import java.io.IOException; import java.security.NoSuchAlgorithmException; import java.security.InvalidKeyException; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; import javax.crypto.BadPaddingException; import javax.crypto.IllegalBlockSizeException; import javax.crypto.spec.SecretKeySpec; import javax.xml.bind.DatatypeConverter; import sun.misc.BASE64Encoder; import sun.misc.BASE64Decoder; public class AESEncryptor { private static final String symKeyHex = "DA010203B405060708011E020C0D0E04"; private static String strCipherText = new String(); private static String strDecryptedText = new String(); //private KeyGenerator keyGen; private static SecretKey secretKey; private static Cipher aesCipher; private static byte[] byteDataToEncrypt; private static byte[] byteDecryptedText; private static byte[] byteCipherText; private byte[] symKeyData; public AESEncryptor() { try { //Generate an AES key using KeyGenerator Initialize the keysize to 128 //keyGen = KeyGenerator.getInstance("AES"); //keyGen.init(128); //secretKey = keyGen.generateKey(); //Create SecretKey from hex bytes String symKeyData = DatatypeConverter.parseHexBinary(symKeyHex); secretKey = new SecretKeySpec(symKeyData, "AES"); aesCipher = Cipher.getInstance("AES"); } catch (NoSuchAlgorithmException noSuchAlgo) { System.out.println(" No Such Algorithm exists " + noSuchAlgo); } catch (NoSuchPaddingException noSuchPad) { System.out.println(" No Such Padding exists " + noSuchPad); } } public String encrypt(String strDataToEncrypt) { try { aesCipher.init(Cipher.ENCRYPT_MODE, secretKey); byteDataToEncrypt = new BASE64Decoder().decodeBuffer(strDataToEncrypt); byteCipherText = aesCipher.doFinal(byteDataToEncrypt); strCipherText = new BASE64Encoder().encode(byteCipherText); } catch (BadPaddingException badPadding) { System.out.println(" Bad Padding " + badPadding); } catch (IllegalBlockSizeException illegalBlockSize) { System.out.println(" Illegal Block Size " + illegalBlockSize); } catch (IOException e) { e.printStackTrace(); } catch (InvalidKeyException e) { e.printStackTrace(); } return strCipherText; } public String decrypt(String strCipherText) { try { aesCipher.init(Cipher.DECRYPT_MODE, secretKey, aesCipher.getParameters()); byteCipherText = new BASE64Decoder().decodeBuffer(strCipherText); byteDecryptedText = aesCipher.doFinal(byteCipherText); strDecryptedText = new String(byteDecryptedText); } catch (Exception e) { e.printStackTrace(); } return strDecryptedText; } }