MySQL tutorial: ASYMMETRIC_ENCRYPT [EN]
top of page
CerebroSQL

MySQL: 

ASYMMETRIC_ENCRYPT

Syntax:
asymmetric_encrypt(algorithm, str, key_str)

Encrypts a string using the given algorithm and key string, and returns
the resulting ciphertext as a binary string. If encryption fails, the
result is NULL.

The str length cannot be greater than the key_str length ? 11, in
bytes

key_str must be a valid key string in PEM format. algorithm indicates
the encryption algorithm used to create the key.

Supported algorithm values: 'RSA'

To encrypt a string, pass a private or public key string to
asymmetric_encrypt(). To recover the original unencrypted string, pass
the encrypted string to asymmetric_decrypt(), along with the public or
private key string correponding to the private or public key string
used for encryption.

Example

-- Generate private/public key pair
SET @priv = create_asymmetric_priv_key('RSA', 1024);
SET @pub = create_asymmetric_pub_key('RSA', @priv);

-- Encrypt using private key, decrypt using public key
SET @ciphertext = asymmetric_encrypt('RSA', 'The quick brown fox', @priv);
SET @plaintext = asymmetric_decrypt('RSA', @ciphertext, @pub);

-- Encrypt using public key, decrypt using private key
SET @ciphertext = asymmetric_encrypt('RSA', 'The quick brown fox', @pub);
SET @plaintext = asymmetric_decrypt('RSA', @ciphertext, @priv);

bottom of page