gen_salt postgresql pgcrypto

108 views Asked by At

I'm studying pgcrypto and I'm interested in:

(crypt('123@', gen_salt('bf', 4)))

I can get a boolean value if I do:

SELECT * FROM user WHERE password = crypt('123@', password)

But if I want to decrypt the amount I entered in my bank? how do I do ? I saw that we have the pgp_sym_encrypt() function

but this function needs a 'secret-key' to decrypt, I didn't find it that interesting Is there a way to decrypt something that was generated by gen_salt?

1

There are 1 answers

0
Maimoona Abid On BEST ANSWER

You can try following code to encrypt and decrypt your data securely by using postgres pgcrypto extension;

-- Encrypting data using pgp_sym_encrypt
INSERT INTO encrypted_data (encrypted_amount) VALUES (pgp_sym_encrypt('1000', 'secret_key'));

-- Decrypting data using pgp_sym_decrypt
SELECT pgp_sym_decrypt(encrypted_amount, 'secret_key') FROM encrypted_data;

Hope it works :)