1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Rep Power

    Encryption/Decryption Issue

    Hello everyone,

    I'm trying to take an existing client/server chat client and encrypt the information before it goes across (This is working fine) and decrypt it when it comes in. (Here in lies the issue)

    The exact problem occurs at the line:
    ptLength += cipher.doFinal(plainText, outputSize);
    It is a problem with the "doFinal" function and no matter what I do it always throws the exception. I've even tried hard coding in the buffer length (the second argument in doFinal) to the correct size, and this still doesn't work. I'm completely stumped and I'm getting quite frustrated. Have been troubleshooting this for HOURS.

    Here is the entire readLine while loop:

    String s;
            while ((s = in.readLine()) != null)
    				//Beginning of the AES Decryption process
    				byte[] input = s.getBytes();
    				byte[] cipherText = new byte[input.length];
    				SecretKeySpec key = new SecretKeySpec(keyBytes, "AES");
    				Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding", "BC");
    				int ctLength = s.length();
    				byte[] plainText = new byte[ctLength];
    				cipher.init(Cipher.DECRYPT_MODE, key);
    				int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0);
    				int outputSize = cipher.getOutputSize((ctLength/16 + 1) * 16);
    				ptLength += cipher.doFinal(plainText, outputSize);
    				System.out.println("plain text : " + new String(plainText) + " bytes: " + ptLength);
    				//End of the AES Decryption process
    				String encryptedText = new String(cipherText);
    				enteredText.setText(encryptedText + "\n");
    				enteredText.insert(new String(plainText) + "\n", new String(plainText).length());
    				enteredText.setCaretPosition(new String(plainText).length());
    			catch (Exception ace)
    				enteredText.setText("Error during AES Decryption process");

    I will upload the entire program if need be, although it's rather large and in many different files.

    I appreciate any help

    Thank you,
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Eastern Florida
    Rep Power
    it always throws the exception
    Can you post the full text of the exception?

    Does the technique and methods you are using work in a small standalone program without the client/server features?

    Have you printed out the bytes that were sent and the bytes that are received and compared them to make sure they are identical?

IMN logo majestic logo threadwatch logo seochat tools logo