Problem decrypting with AES
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.
Code :
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;
}
}
Re: Problem decrypting with AES
Do you have code for testing?
What are the definitions for the arguments passed to the methods?
Re: Problem decrypting with AES
I just tested with something like:
String encryptedString = encrypt("test123");
String decryptedString = decrypt(encryptedString);
Eitherway, I managed to solve the problem myself. I just removed the aesCipher.getParameters() parameter when initialising the cipher and it now works perfectly.
Re: Problem decrypting with AES
Strange. Can you post the new code that now works?
I tried it a different way and got it to work.
Re: Problem decrypting with AES
Here's what I changed the decrypt method to:
Code :
public String decrypt(String strCipherText)
{
try
{
aesCipher.init(Cipher.DECRYPT_MODE, secretKey);
byteCipherText = new BASE64Decoder().decodeBuffer(strCipherText);
byteDecryptedText = aesCipher.doFinal(byteCipherText);
strDecryptedText = new String(byteDecryptedText);
}
catch (Exception e)
{
e.printStackTrace();
}
return strDecryptedText;
}
Re: Problem decrypting with AES
Can you post a full program that compiles, executes and shows the results. With only part of the code you are never sure that you are testing the exact same code.