Decrypting an unencrypted string

Hi All,

I have coded a method that takes an input and decrypts it. It works in most cases but sometimes, if the input is not encrypted, it still tries to decrypt it and returns a garbage value.

Can anyone let me know how to check if the given string is encoded. I can make this check and then go ahead for decrypting. Or is there a better way to do this.

My method is as below :

public static String decrypt(String input) {

if (input == null){

return null;

}

if (input.trim().equals("")){

return "";

}

String result = null;

try {

boolean isDecoded = true;

byte[] bOut = null;

cipher.init(Cipher.DECRYPT_MODE, key);

// decrypt

try {

bOut = cipher.doFinal(new Base64().decode(input.getBytes("UTF-8")));

}catch(Exception e) {

isDecoded = false;

}

if(!isDecoded) {

String urlDeCoded = URLDecoder.decode(input,"UTF-8");

bOut = cipher.doFinal(new Base64().decode(urlDeCoded.getBytes("UTF-8")));

}

result = new String(bOut, "UTF-8");

}catch(UnsupportedEncodingException ex) {

return input;

}catch(BadPaddingException ex) {

return input;

}catch(InvalidKeyException ex) {

return input;

}catch(IllegalBlockSizeException ex) {

return input;

}

return result;

}

If I send an input '2000100010' , it returns a garbage String '?6?'

Thanks ,

Santunu

[1485 byte] By [santunupa] at [2007-11-27 9:07:10]
# 1
Test to see if the String contains all alphanumeric values by looping through and making sure each ASCII value is in the range you're expecting.
C.R.Crena at 2007-7-12 21:43:32 > top of Java-index,Security,Cryptography...
# 2

> Test to see if the String contains all alphanumeric

> values by looping through and making sure each ASCII

> value is in the range you're expecting.

The base64 character set is more than that. It also contains '+','/' and '='. Optionally it may also contain '\r' and '\n'.

sabre150a at 2007-7-12 21:43:32 > top of Java-index,Security,Cryptography...
# 3

> The base64 character set is more than that. It also

> contains '+','/' and '='. Optionally it may also

> contain '\r' and '\n'.

Right, but you can still retrieve binary representations and test against them, correct? And if you know what values you'll be expecting, then you have something to test against.

C.R.Crena at 2007-7-12 21:43:32 > top of Java-index,Security,Cryptography...