Open aubrey-fowler opened 7 years ago
Any luck?
Not really, since the encrypted text is a normal string and you can't really tell out of the box what encryption method was used, would be my vague guess.
I'm also in need of this. I think you could check the sigBytes
property of bytes
in your example. If it's a negative number, then it wasn't an encrypted string. If it's positive, it was. But I'm not 100% sure that's accurate. So far in my testing it checks out.
@benwilkins did you find this effective?
Yes. It's been a while, but I think I remember that this is how I accomplished it.
It is not 100% accurate. From my test, it returned a positive number for a non-encrypted string.
When you try to decrypt a not encrypted value, you get empty value on decrypted.toString(). Then you can check if ur decrypt.toString() have some value or not,
var myString = "Hello World"
var encrypted = Crypto.JS.AES.encrypt(myString, myPassword);
var decrypted = CryptoJS.AES.decrypt(myString, myPassword);
var value=decrypted.toString(CryptoJS.enc.Utf8);
if (value)
{
// Dont do nothing because myString is already an encrypted value
} else
{
// Here encrypt your string again
}
I tried the approach mentioned here
var myString = "Hello World"
var encrypted = Crypto.JS.AES.encrypt(myString, myPassword);
var decrypted = CryptoJS.AES.decrypt(myString, myPassword);
var value=decrypted.toString(CryptoJS.enc.Utf8);
if (value)
{
// Dont do nothing because myString is already an encrypted value
} else
{
// Here encrypt your string again
}
but on few occasions, value is a non empty string, and that causes this logic to fail. Anyone else seen this issue?
This is a tough cookie... the way im doing it is
return my encryption as base64
return encrypted.toString('base64')
then i can always test if my incoming string to decrypt is base64
isBase64(text){ return /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/.test(text); }
// if text is not base64 then its not encrypted therefore we should not attempt to decrypt it
if ((text === undefined) || (text === '') || (text === null) || (!this.isBase64(text)))
throw new Exception('3101', Exception.EncryptionException, 'text to decrypt is invalid');
I solved this problem by creating a function to check if it is encrypted, permeated with a try catch to solve the Malformed UTF-8 data error that sometimes happens
const hashKey = test
const SampleFunction = (string: string): boolean => {
const bytes = CryptoJS.AES.decrypt(string, hashKey)
try {
const decrypted = bytes.toString(CryptoJS.enc.Utf8)
return !!decrypted
}
catch (error) {
return false
}
}
And calling to verify:
SampleFunction(stringToTest)
Testing this way, if the string is not encrypted, it will return empty in the bytes.toString(CryptoJS.enc.Utf8) method. If not, it will return a string.
In both situations, it will be converted to a boolean value, and in case of error, it is also considered as an unencrypted value, as it was not possible to decrypt using the reverse method.
Use this to check, works in 2023. When you try decrypting an un-encrypted string it returns an empyt string.
after decryption use
if ( decryptedString === "" ) { console.log("This string is not encrypted"); }
// For foolproof check use: if ( decryptedString.length < expectedMinimumLengthOfDecryptedString ) { console.log("This string is not encrypted"); }
This code works great for me:
Is there a way to check if a string has already been encrypted?