AES_ENCRYPT function [String]

Function

Encrypts the specified values using the supplied encryption key, and returns a VARBINARY or LONG VARBINARY.

Syntax

AES_ENCRYPTstring-expression, key )

Parameters

string-expression The data to be encrypted. For a list of supported data types, see “Data types for encrypted columns”. Binary values can also be passed to AES_ENCRYPT. This parameter is case-sensitive, even in case-insensitive databases.

key The encryption key used to encrypt the string-expression. To obtain the original value, you must also use the same key to decrypt the value. This parameter is case sensitive, even in case-insensitive databases.

As with most passwords, it is best to choose a key value that is difficult to guess. Sybase recommends that you choose a value for your key that is at least 16 characters long, contains a mix of uppercase and lowercase letters, and includes numbers and special characters. You need this key each time you want to decrypt the data.

WARNING! Protect your key; store a copy of your key in a safe location. If you lose your key, encrypted data becomes completely inaccessible and unrecoverable.

Usage

AES_ENCRYPT returns a VARBINARY value, which is at most 31 bytes longer than the input string-expression. The value returned by this function is the ciphertext, which is not human-readable. You can use the AES_DECRYPT function to decrypt a string-expression that was encrypted with the AES_ENCRYPT function. To successfully decrypt a string-expression, use the same encryption key and algorithm used to encrypt the data. If you specify an incorrect encryption key, an error is generated.

If you are storing encrypted values in a table, the column should be of data type VARBINARY or VARCHAR, and greater than or equal to 32 bytes, so that character set conversion is not performed on the data. (Character set conversion would prevent decryption of the data.) If the length of the VARBINARY or VARCHAR column is less than 32 bytes, then the AES_DECRYPT function returns an error.

The result data type of an AES_ENCRYPT function may be a LONG VARBINARY. If you use AES_ENCRYPT in a SELECT INTO statement, you must have a Large Objects Management Option license, or use CAST and set AES_ENCRYPT to the correct data type and size.

For additional details and usage information, see “REPLACE function [String]”in Chapter 4, “SQL Functions” in Reference: Building Blocks, Tables, and Procedures.

Standards and compatibility

See also

“AES_DECRYPT function [String]”

“LOAD TABLE ENCRYPTED clause”

Example

See “Encryption and decryption examples” for an example of the use of the AES_ENCRYPT function.