I am using the following code for encrypting my data:
public static String encrypt(String clearData, String password)
throws UnsupportedEncodingException {
byte[] bytes = encrypt(clearData.getBytes("UTF-8"), password);
return Base64.encodeBytes(bytes);
}
public static byte[] encrypt(byte[] clearData, String password)
throws UnsupportedEncodingException {
byte[] passwordKey = encodeDigest(password);
try {
aesCipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
} catch (NoSuchAlgorithmException e) {
Log.e(TAG, "No such algorithm " + CIPHER_ALGORITHM, e);
} catch (NoSuchPaddingException e) {
Log.e(TAG, "No such padding PKCS5", e);
}
secretKey = new SecretKeySpec(passwordKey, CIPHER_ALGORITHM);
try {
aesCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
} catch (InvalidKeyException e) {
Log.e(TAG, "Invalid key", e);
return null;
} catch (InvalidAlgorithmParameterException e) {
Log.e(TAG, "Invalid algorithm " + CIPHER_ALGORITHM, e);
return null;
}
byte[] encryptedData;
try {
encryptedData = aesCipher.doFinal(clearData);
} catch (IllegalBlockSizeException e) {
Log.e(TAG, "Illegal block size", e);
return null;
} catch (BadPaddingException e) {
Log.e(TAG, "Bad padding", e);
return null;
}
return encryptedData;
}
How can I check that an input string data is encrypted or not without using try-catch
?
Update:
for example the user gives me a string. If it is encrypted I store it in database if not I encrypt it then store.