0

I have a PostgreSQL database where I use the pgp_sym_encrypt() function to crypt data

INSERT INTO public.product(name, brand, size, price)
    VALUES (pgp_sym_encrypt('product1', 'AES_KEY'), 'brand 1', 1, 1);

And it works with pgp_sym_decrypt()

SELECT pgp_sym_decrypt(name::bytea, 'AES_KEY') FROM public.product;

Is there some equivalent code in c# to decrypt the data which are crypted with the pgp_sym_encrypt() function ?

I tried this kind of code but I always get the crypted data

using (Aes aes = Aes.Create())
{
    aes.Key = Encoding.UTF8.GetBytes("AES_KEY");
    aes.IV = new byte[16];
    ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

    var buffer = Encoding.UTF8.GetBytes(data);

    using (MemoryStream memoryStream = new MemoryStream(buffer))
    {
        using (CryptoStream cryptoStream = new CryptoStream((Stream)memoryStream, decryptor, CryptoStreamMode.Read))
        {
            using (StreamReader streamReader = new StreamReader((Stream)cryptoStream))
            {
             return streamReader.ReadToEnd();
            }
        }
    }
}
Timothy G.
  • 6,335
  • 7
  • 30
  • 46
  • You aren't using `AES` despite the `'AES_KEY'` you put in the code, you are using PGP/ – Charlieface Sep 09 '22 at 14:11
  • Does this answer your question? [.net core PGP Encryption Decryption](https://stackoverflow.com/questions/52224818/net-core-pgp-encryption-decryption) – Charlieface Sep 09 '22 at 14:11
  • @Charlieface you mean PGP on the PostgreSQL side ? Even if i use `pgp_sym_encrypt('product1', 'AAECAwQFBgcICQoLDA0ODw==','cipher-algo=aes128')` ? – Blancassis Sep 09 '22 at 14:22
  • You didn't say that. But it would still be a PGP message, which means you need to decrypt it using PGP functions. Why don't you just decrypt it in Postgres? – Charlieface Sep 09 '22 at 14:24
  • So it will be always a PGP message ? IT explains a lot ! – Blancassis Sep 09 '22 at 15:36

0 Answers0