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

