Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Problem decrypting with AES

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

    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;
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: Problem decrypting with AES

    Do you have code for testing?
    What are the definitions for the arguments passed to the methods?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem decrypting with AES

    Here's what I changed the decrypt method to:

    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;
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,195
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 12:44 AM