SoFunction
Updated on 2025-04-11

Common ways PostgreSQL can effectively handle encryption and decryption of data

For projects with high security requirements, sensitive data must be stored encrypted and encrypted.

The encryption and decryption of processing data in PostgreSQL can be implemented in a variety of ways to ensure data confidentiality and security.

I provide several common methods here.

1. Use pgcrypto extension

pgcrypto is a commonly used extension in PostgreSQL that provides encryption and decryption capabilities.

Install the pgcrypto extension

First, you need to make sure that the pgcrypto extension is installed. You can use the following command to install in the database:

CREATE EXTENSION pgcrypto;

Symmetric encryption (using AES algorithm)

The following is usedpgcryptoSample code for extending symmetric encryption (AES):

-- encryption
SELECT encrypt('Hello World', 'y_secret_key', 'aes');

-- Decryption
SELECT decrypt(encrypt('Hello World', 'y_secret_key', 'aes'), 'y_secret_key', 'aes');

In the above example, 'my_secret_key' is the encryption key of your choice to encrypt and decrypt data. AES algorithms usually provide better security and performance balance.

explain:

The encrypt function accepts the data to be encrypted, the encryption key, and the encryption algorithm as parameters, and returns the encrypted result.
The decrypt function accepts the encrypted result, encryption key, and encryption algorithm for decryption, and returns the original data.

Asymmetric encryption (using RSA algorithm)

Example of asymmetric encryption (RSA) using pgcrypto extension:

-- generate RSA Key pair
SELECT gen_rsa_private_key(2048) AS private_key, gen_rsa_public_key(2048) AS public_key;

-- encryption
SELECT encrypt_rsa('Hello World', public_key) AS encrypted_data 
FROM (SELECT gen_rsa_public_key(2048) AS public_key) t;

-- Decryption
SELECT decrypt_rsa(encrypted_data, private_key) AS decrypted_data
FROM (
    SELECT 
        encrypt_rsa('Hello World', gen_rsa_public_key(2048)) AS encrypted_data,
        gen_rsa_private_key(2048) AS private_key
) t;

explain:

  • gen_rsa_private_keyandgen_rsa_public_keyFunctions are used to generate RSA key pairs of specified lengths.
  • encrypt_rsaFunctions encrypt data using public keys.
  • decrypt_rsaThe function uses a private key to decrypt the encrypted data.

2. Custom functions to implement encryption and decryption

In addition to usingpgcryptoThe functions provided by the extension can also customize functions according to business needs to implement more complex encryption and decryption logic.

Here is a simple example of simple replacement encryption using custom functions:

CREATE OR REPLACE FUNCTION custom_encrypt(text_to_encrypt text)
RETURNS text AS $$
DECLARE
    encrypted_text text := '';
    char_code integer;
BEGIN
    FOR i IN 1..length(text_to_encrypt) LOOP
        char_code := ascii(substring(text_to_encrypt, i, 1)) + 1;
        encrypted_text := encrypted_text || chr(char_code);
    END LOOP;
    RETURN encrypted_text;
END;
$$ LANGUAGE plpgsql;
 
CREATE OR REPLACE FUNCTION custom_decrypt(encrypted_text text)
RETURNS text AS $$
DECLARE
    decrypted_text text := '';
    char_code integer;
BEGIN
    FOR i IN 1..length(encrypted_text) LOOP
        char_code := ascii(substring(encrypted_text, i, 1)) - 1;
        decrypted_text := decrypted_text || chr(char_code);
    END LOOP;
    RETURN decrypted_text;
END;
$$ LANGUAGE plpgsql;

Example of usage:

SELECT custom_encrypt('Hello World');
SELECT custom_decrypt(custom_encrypt('Hello World'));
 

explain:

In the above custom functions,custom_encryptThe function increases the ASCII code value of each character in the input text by 1 for encryption.custom_decryptThe function reduces the ASCII code value of the encrypted characters by 1 for decryption.

This is the article about common methods of PostgreSQL to effectively process data encryption and decryption. This is the end of this article. For more information about PostgreSQL's encryption and decryption of data processing, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!