brix / crypto-js

JavaScript library of crypto standards.
Other
15.82k stars 2.38k forks source link

How to check if a string is already encrypted? #127

Open aubrey-fowler opened 7 years ago

aubrey-fowler commented 7 years ago

This code works great for me:

        // Encrypt
        var ciphertext = CryptoJS.AES.encrypt('my message', 'secret key 123');
        console.log(ciphertext.toString());

        // Decrypt
        var bytes  = CryptoJS.AES.decrypt(ciphertext.toString(), 'secret key 123');
        var plaintext = bytes.toString(CryptoJS.enc.Utf8);
        console.log(plaintext);

Is there a way to check if a string has already been encrypted?

dwaita commented 6 years ago

Any luck?

NuclearPhoenixx commented 6 years ago

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.

benwilkins commented 6 years ago

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.

jsmmth commented 5 years ago

@benwilkins did you find this effective?

benwilkins commented 5 years ago

Yes. It's been a while, but I think I remember that this is how I accomplished it.

leonardyhuang commented 4 years ago

It is not 100% accurate. From my test, it returned a positive number for a non-encrypted string.

mariodiazd commented 4 years ago

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

}
navin-vts commented 2 years ago

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?

Muhammad-1990 commented 2 years ago

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');
espindolacarlos commented 1 year ago

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.

OssaiJE commented 1 year ago

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"); }