I have encrypted mutiple strings one-by-one in using crypto-js in react.'
For encryption I used -
encryptAES = (text, key) => {
return CryptoJS.AES.encrypt(text, key).toString();
};
For decryption, I used function like following -
decryptAES = (encryptedBase64, key) => {
const decrypted = CryptoJS.AES.decrypt(encryptedBase64, key);
if (decrypted) {
try {
console.log(decrypted);
const str = decrypted.toString(CryptoJS.enc.Utf8);
if (str.length > 0) {
return str;
} else {
return 'error 1';
}
} catch (e) {
return 'error 2';
}
}
return 'error 3';
};
I have uploaded a working sample project of this encryption - decryption here.
For e.g., if I encrypt "I live in India" using key - "earth", it would output as - "U2FsdGVkX1+cBvU9yH5fIGVmliJYPXsv4AIosUGH4tA=", and similary it would decrypt successfully with the correct key.
Now I have multiple encrypted strings stored in my database, but now require them to store un-encrypted, so I wanted to decrypt them in PHP. I can decrypt them in js using the function mentioned above but I am unable to figure out how to do so in PHP. I have tried this github repository but I couldn't customize it for my use case.
U2FsdGVkX1indicates that the key material was passed as string during encryption. In this case CryptoJS applies a key derivation function (EVP_BytesToKey()), which is not a standard and has to be implemented explicitly in the PHP code (you can find PHP implementations on the web). The at once is not clear to me, you have to decrypt each ciphertext with its corresponding key individually.PHPfunction to decrypt it. I even tried to read aboutEVP_BytesToKey()as you mentioned, but I am unable to figure out how to implement it inPHP. For e.g. if I want to decryptU2FsdGVkX1+cBvU9yH5fIGVmliJYPXsv4AIosUGH4tA=with its keyearth, what should be my PHP code.