%encrypt() function

The %encrypt() function encrypts a string.

Syntax

code = %encrypt(params, inputstring, outputstring, &outputstring length)

Parameter(s)

code Return code representing the result of the function.
params A dynamic array containing the following information:
Algorithm (Supported algorithm is Crypto$Algorithm$AES128).
Length of input string.
Size of output buffer.
Clear text key (must be 16-bytes long). If the clear text key is null, the default key will be used.
inputstring Input string.
outputstring Output string.
outputstring length Length of the encrypted string.

Description

This function returns an integer to the code variable as follows:

0 Success
-1 Error
-2 Invalid keysize (must be 16 byte)
-3 Output buffer too small. The output string buffer must be pre-allocated, and its size must be a minimum of twice the size of the input buffer for the %encrypt() function to work. The return parameter outputstring length denotes the actual length of the encrypted string.
-4 Encryption libraries are not loaded.

Example(s)

The example below illustrates encrypting and decrypting a user-provided string.

include dm,bp,includes nt_util.inc
include dm,bp,includes crypto.inc ;
crt "enter string: ":; input password
crt "enter 16-char key: ":; input key
char encryptedPassword[ 1 * Len( password ) ]
cryptoParams = ""
cryptoParams< Crypto$P$Algorithm > =Crypto$Algorithm$AES128
cryptoParams< Crypto$P$inputlength > = Len( password )
cryptoParams< Crypto$P$outputLength > = Len(encryptedPassword )
cryptoParams< Crypto$P$ClearKey > = key
encryptedLen = 0
gp.result = %Encrypt( cryptoParams, password,encryptedPassword, &encryptedLen)
encryptedPassword = encryptedPassword[1, encryptedLen]
*
* now do the decryption
char decryptedPswd[Len(encryptedPassword)]
cryptoParams< Crypto$P$inputLength > = encryptedLen
cryptoParams< Crypto$P$outputLength > = Len(decryptedPswd )
decryptedLen = 0
gp.result = %Decrypt( cryptoParams, encryptedPassword,decryptedPswd, &decryptedLen)
if (password = decryptedPswd) then print "pass" else print "fail"