1

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.

Aurel Drejta
  • 361
  • 2
  • 11

2 Answers2

0

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Tom Ruyter
  • 176
  • 2
  • 9
  • I tried this: aes_decrypt(from_base64('U2FsdGVkX1/BpEUjr5y+hivlNpUep+HZQG4Tw8bmTvQ='), 'secretkey') It still returns null though. However now when I look at the new value created from the from_base64() method i get something like this : Slated__XXXXXXXXX (where X are some random characters) – Aurel Drejta Sep 20 '19 at 10:23
  • Looks like the 'cryptoJS.AES' is storing the Salt and the value in one string which means it's going to need parsing by a routine before you can access it in mySQL. if you look at the steps that they go though in [link](https://stackoverflow.com/questions/14958103/how-to-decrypt-message-with-cryptojs-aes-i-have-a-working-ruby-example) to get it to work, it looks like you'll have to do something similar. This isn't just a straight forward AES decode like your mysql routine is expecting. – Tom Ruyter Sep 20 '19 at 10:48
  • So there is no way to do it in mysql directly? I hope there is , otherwise I guess i'm gonna have to decrypt them with cryptoJs and then encrypt them again using mysql. I absolutely need to do the decryption in mysql as i have to do some order by's and other calculations. – Aurel Drejta Sep 20 '19 at 12:09
0

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