I have a scenario where I have to match the password/sensitive information and check for the validation. Now there are two ways here I can come up with.
1) We can compare the password/sensitive field in the encrypted form by fetching the right password from DB which is stored there in encrypted form.
2) Or we can decrypt the passwords first in to the plain text form and then compare them. Now in this scenario there is an extra call to decrypt utility, which sort of an overhead.
I have looked at the "equals()" method of String class which runs in amortized constant time. So if the encrypted string is insanely long string then it will have impact on the performance of the "equals()" method. But here in my case encrypted strings are not so long.
But my main concern is what is the standard generally followed. UPDATE 1:
public static String encrypt(String text, String algo, byte[] bytes) throws NoSuchPaddingException, NoSuchAlgorithmException,
IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
if (text == null || algo == null || bytes == null) {
//log properly
System.out.println("Please provide a valid text, algo or key bytes");
return null;
}
Key key = generateKey(bytes, algo);
Cipher c = Cipher.getInstance(algo);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] envValue = c.doFinal(text.getBytes());
String encVal2 = new BASE64Encoder().encode(envValue);
return encVal2;
}`
Update 2:
while (i < 300000) {
String value = KeyValidator.getSystemLicKey(KEY_);
if (!value.equals(value1))
System.out.println("--- : false");
value1 = value;
i++;
}