1
votes

In my database i have a column that stores cryptojs encrypted values in this type of form:

U2FsdGVkX1/BpEUjr5y+hivlNpUep+HZQG4Tw8bmTvQ=

When i decrypt this with cryptojs it's all good.

let decryptedValue = cryptoJS.AES.decrypt(
    encryptedField,
    secretKey
 );

But decrypting it with mysql returns null.

I've tried converting to the encrypted string into binary then decrypting it with mysql's aes_decrypt() but it returns null.

SELECT convert(AES_DECRYPT(binary(encrypted_field),'secret_key') using utf8) as decrypt

The encrypted values are numbers and i need to decrypt them in order to do some calculations in mysql with them.

I don't know if there is something wrong with the way i'm decrypting it or do cryptojs and mysql have different ways of encrypting and decrypting using AES.

Thank you in advance.

2

2 Answers

0
votes

It looks like your initial string is Base64 Encoded. It's possible that cryptoJS.AES or some other process is taking the encrypted bytes and storing them as Base64 in the database.

Your databases decrypt routine is probably assuming the raw bytes, so you'll need to turn the Base64 string (check the net for mysql's base64 routines) into the actual bytes and then pass them to the AES_DECRYPT function.

0
votes

Not sure if your question still remains but here is a suggestion to use an alternate to CryptoJS. I found a Node JS implementation of MySQL AES encryption & decryption. This library helped me to encrypt data that is readable in MySQL too.

Here is the link:

https://www.npmjs.com/package/mysql-aes