简体   繁体   中英

Decrypt AES in mysql that was encrypted with CryptoJS

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM