0

There is no problem to decrypt the encrypted string using crypto-js.

My code is here :

'use strict';

const cryptoJs = require('crypto-js');

const key = 'myTestKey';  

const encMyPassword = cryptoJs.AES.encrypt('myPassword', key).toString();
console.log(`encrypted : ${encMyPassword}`);

const decMyPassword = cryptoJs.AES.decrypt(encMyPassword, key).toString(cryptoJs.enc.Utf8);
console.log(`decrypted : ${decMyPassword}`);

Output:

encrypted : U2FsdGVkX19XxsV6RGCXGcy7ySNarTOa4o0+uAWGJZY=
decrypted : myPassword

I tried to decrypt the encrypted string in postgresql, but it fails.

select decrypt('U2FsdGVkX19XxsV6RGCXGcy7ySNarTOa4o0+uAWGJZY='::bytea, 'myTestKey'::bytea, 'aes');

Output:

SQL Error [39000]: ERROR: decrypt error: Data not a multiple of block size

How do I fix my query?

James Kim
  • 23
  • 5
  • 1
    I wouldn't really expect those two functions to be compatible with each other across languages, unless you have some good reason to think they should be. Also, your "encrypted" string looks like base64 encoding, not a standard representation of bytea. – jjanes Feb 09 '22 at 03:08
  • Indeed the default nodejs encrypted format is not what posgresql is using. Maybe you could have a look at a comment from Topaco https://stackoverflow.com/questions/70773578/how-do-i-decrpyt-a-message-with-aes-when-i-have-only-the-encrypted-message-and-a – gusto2 Feb 09 '22 at 06:56

0 Answers0