Example code using the ENCRYPT and DECRYPT functions
- Last Updated: January 17, 2024
- 2 minute read
- OpenEdge
- Version 12.8
- Documentation
This section shows basic uses of the ABL symmetric cryptography functions and related ABL elements.
The following example shows the use of a random binary key to
encrypt the text "Bathtub Pancake Ladybug".
Example 1: Encryption using a random binary key
|
For the receiver of the crypto-value to be able to successfully decrypt the value and recover the clear text requires the following information:
- The name of the algorithm (the default,
AES_CBC_128, used and obtained by readingSECURITY-POLICY:SYMMETRIC-ENCRYPTION-ALGORITHM) - The initialization vector (none used, but available by reading
SECURITY-POLICY:SYMMETRIC-ENCRYPTION-IV) - The binary key valueNote: Setting
SECURITY-POLICY:SYMMETRIC-ENCRYPTION-KEYdirectly from theGENERATE-RANDOM-KEYfunction leads to an irrecoverable key because this attribute is write-only andGENERATE-RANDOM-KEYreturns a different value if called again.
The following example shows the use of a binary key to decrypt the text from the previous example.
Example 2: Decryption using a random binary key
|
The following example shows the use of a password-based key to
encrypt the text, "Matter Property Mass Solid".
The password is "Migratory Blueberries".
Example 3: Encryption using a password-based key
|
For the receiver of the crypto-value to be able to successfully decrypt the value and recover the clear text, the following information is necessary:
- The name of the algorithm (a non-default value specified
and obtained by reading
SECURITY-POLICY:SYMMETRIC-ENCRYPTION-ALGORITHM) - The initialization vector (none is used, but can be set using
SECURITY-POLICY:SYMMETRIC-ENCRYPTION-IV) - The number of hashing algorithm iterations to generate the key
(a non-default value specified and obtained by reading
SECURITY-POLICY:PBE-KEY-ROUNDS) - The salt value (obtained by reading
SECURITY-POLICY:ENCRYPTION-SALT)Note: CallingGENERATE-PBE-SALTfunction again produces a different salt value and is not useful in generating the binary key necessary to decrypt the text. - The hash algorithm used to transform the password into the binary key
(a non-default value specified and obtained by reading
SECURITY-POLICY:PBE-HASH-ALGORITHM) - The password text
In place of items mentioned in points 3 through 6, the binary
key value itself can be supplied. Because SECURITY-POLICY:SYMMETRIC-ENCRYPTION-KEY is
write-only, an intermediate RAW variable can be
used to hold the value returned from the GENERATE-PBE-KEY function,
or the function may be called again with the same password value
(it will return the same result provided the SECURITY-POLICY system
handle attributes SYMMETRIC-ENCRYPTION-ALGORITHM, PBE-KEY-ROUNDS, PBE-HASH-ALGORITHM,
and ENCRYPTION-SALT all have the same values).
The following example shows the use of a password-based key to
decrypt the text from the previous example. The password is "Migratory Blueberries".
Example 4: Decryption using a password-based key
|