Instalar gpg2. Desde usuario root debe agregar la siguiente linea de comando:

# apt-get install gnupg2

Creación de claves bajo el uso de gpg2. Este comando genera un par de claves que consiste en una clave pública y privada:

gpg2 --gen-key

           Please select what kind of key you want:
           (1) RSA and RSA (default)
           (2) DSA and Elgamal
           (3) DSA (sign only)
           (4) RSA (sign only)
           Your selection? (1)

           RSA keys may be between 1024 and 4096 bits long.
           What keysize do you want? (4096)

           Please specify how long the key should be valid.
            0 = key does not expire
           <n>  = key expires in n days
           <n>w = key expires in n weeks
           <n>m = key expires in n months
           <n>y = key expires in n years
            Key is valid for? (0)

           Is this correct (y/n)? (y)

           GnuPG need construct a user ID to identify your key

           Real name: <Nombre> <Apellido>
           Email address: correo@coreo.cl
           Comment: "PGPCRYPTO - Test"

           Change (N)ame, (E)mail o (C)omment or (O)kay/(Q)uit? (o)


Listar las claves creadas con GPG:

gpg2 --list-key

Exportar clave pública creada a un archivo de texto plano:

gpg2 -a --export correo@correo.cl > public.key

Exportar clave prívada creada a un archivo de texto plano:

gpg2 -a --export-secret-keys correo@correo.cl > private.key

Se le otorgan permisos de escritura y lectura a la llave privada generada. Además es importante tener en cuenta que es necesario conocer exactamente esta llave, ya que, sin ella no se pondrán desencriptar los archivos

chmod 440 private.keys

Intalación de paquete contrib de postgresql. Este contiene como uno de sus módulos PGPCRYPTO. Recordar entrar con atelación a usuario root:

# apt-get install postgresql-contrib

Para listar o ver el contenido del modulo PGPCRYPTO:

ls /usr/share/postgresql/9.3/extension/pgcrypto--1.0.sql
                         o
vim /usr/share/postgresql/9.3/extension/pgcrypto--1.0.sql

Crea una tabla en la base de datos:

CREATE TABLE notes
(
  id integer,
  note bytea,
  description text,
  CONSTRAINT pk_notes PRIMARY KEY (id)
)

Funciones:

create or replace function get_my_public_key() returns text as $$
return open('/path/to/public.key').read()
$$
language plpythonu;
revoke all on function get_my_public_key() from public;
create or replace function get_my_secret_key() returns text as $$
return open('/path/to/private.key').read()
$$
language plpythonu;
revoke all on function get_my_secret_key() from public;

CREATE OR REPLACE FUNCTION public.encrypt_using_my_public_key(
    IN cleartext text,
    OUT ciphertext bytea)
  RETURNS bytea AS
$BODY$
DECLARE
pubkey_bin bytea;
BEGIN
-- text version of public key needs to be passed through function dearmor() to get to raw key
pubkey_bin := dearmor(get_my_public_key());
ciphertext := pgp_pub_encrypt(cleartext, pubkey_bin);
END;
$BODY$
  LANGUAGE plpgsql VOLATILE SECURITY DEFINER
  COST 100;
ALTER FUNCTION public.encrypt_using_my_public_key(text)
  OWNER TO <user>;
GRANT EXECUTE ON FUNCTION public.encrypt_using_my_public_key(text) TO <user>;
REVOKE ALL ON FUNCTION public.encrypt_using_my_public_key(text) FROM public;

CREATE OR REPLACE FUNCTION public.decrypt_using_my_secret_key(
    IN ciphertext bytea,
    OUT cleartext text)
  RETURNS text AS
$BODY$
DECLARE
secret_key_bin bytea;
secret_phase text;
BEGIN
-- text version of secret key needs to be passed through function dearmor() to get to raw binary key
secret_key_bin := dearmor(get_my_secret_key());
secret_phase = 'YOUR SECRET PASSWORD KEY';
cleartext := pgp_pub_decrypt(ciphertext, secret_key_bin, secret_phase);
END;
$BODY$
  LANGUAGE plpgsql VOLATILE SECURITY DEFINER
  COST 100;
ALTER FUNCTION public.decrypt_using_my_secret_key(bytea)
  OWNER TO <user>;
GRANT EXECUTE ON FUNCTION public.decrypt_using_my_secret_key(bytea) TO <user>;
REVOKE ALL ON FUNCTION public.decrypt_using_my_secret_key(bytea) FROM public;