Simple Encrypting and Decrypting sample using the ENCRYPT and DECRYPT BIFs

Date:5 September 2008
Product/Release:Visual LANSA V11 SP4
Abstract:Sample Function displaying simple usage of ENCRYPT and DECRYPT
Submitted By:LANSA Technical Support

LANSA provides the BIFs ENCRYPT and DECRYPT which are used to encode and decode text strings.

The following sample code takes a string INPUT and Encrypts the input and generates a encrypted text and then proceeds to decode the encrypted text.

Notes:

  1. In order to use the ENCRYPT and DECRYPT BIFS in RDMLX, a minimum of Version 11 SP4 is required.
  2. Due to potential issues in the usage of BINTOHEX and HEXTOBIN BIFs in conjunction with the ENCRYPT and DECRYPT BIFs, a recent enhancement (introduction of a 4th parameter) has been made to the ENCRYPT and DECRYPT BIFs. It is recommended that the 4th parameter of the ENCRYPT and DECRYPT BIFs is used instead of BINTOHEX and HEXTOBIN BIFs.

Sample code:

FUNCTION OPTIONS(*DIRECT)
DEFINE FIELD(#KEY16) TYPE(*CHAR) LENGTH(016) DESC('Encryption key supplied') DEFAULT('xG5LQPanEIXBhYkf')
DEFINE FIELD(#VAL32) TYPE(*CHAR) LENGTH(32) DESC('Value to decrypted')
DEFINE FIELD(#FP_EKEY) TYPE(*CHAR) LENGTH(32) DESC('Value to decrypted')
DEFINE FIELD(#HTTPUSREM) TYPE(*CHAR) LENGTH(32) DESC('Value to decrypted')
DEFINE FIELD(#ENC32) TYPE(*CHAR) LENGTH(32) DESC('Value in encrypted form')
DEFINE FIELD(#HEX64) TYPE(*CHAR) LENGTH(64) DESC('Encrypted value in Hex')
DEFINE FIELD(#ZZCHAR64) TYPE(*CHAR) LENGTH(64)
DEFINE FIELD(#ZZCHAR32) TYPE(*CHAR) LENGTH(32)
DEFINE FIELD(#DKEY16) TYPE(*CHAR) LENGTH(016) DEFAULT('xG5LQPanEIXBhYkf')
DEFINE FIELD(#DHEX64) TYPE(*CHAR) LENGTH(064) DESC('Encrypted value in hex')
DEFINE FIELD(#DVAL32) TYPE(*CHAR) LENGTH(032) DESC('Decrypted value returned')
DEFINE FIELD(#DENC32) TYPE(*CHAR) LENGTH(032)
DEFINE FIELD(#DLEN) TYPE(*DEC) LENGTH(005) DECIMALS(0)

BEGIN_LOOP
REQUEST FIELDS(#FP_EKEY)
CHANGE FIELD(#HTTPUSREM) TO(#FP_EKEY)
EXECUTE SUBROUTINE(ENCRYPT) WITH_PARMS(#HTTPUSREM #ZZCHAR64)
DISPLAY FIELDS(#ZZCHAR64)
REQUEST FIELDS(#ZZCHAR64)
EXECUTE SUBROUTINE(DECRYPT) WITH_PARMS(#ZZCHAR64 #ZZCHAR32)
DISPLAY FIELDS(#ZZCHAR32)
END_LOOP
RETURN

SUBROUTINE NAME(ENCRYPT) PARMS((#VAL32 *RECEIVED) (#HEX64 *RETURNED))
DEFINE FIELD(#LEN) TYPE(*DEC) LENGTH(5) DECIMALS(0)
CHANGE FIELD(#LEN) TO(32)
* Use ENCRYPT BIF to encrypt #VAL16 of length #LEN using
* #KEY16 to return encrypted value in #ENC16
USE BUILTIN(RIGHT) WITH_ARGS(#VAL32) TO_GET(#VAL32)
USE BUILTIN(ENCRYPT) WITH_ARGS(#VAL32 #LEN #KEY16 YES) TO_GET(#HEX64)
* USE BUILTIN(BINTOHEX) WITH_ARGS(#ENC32) TO_GET(#HEX64)
ENDROUTINE

SUBROUTINE NAME(DECRYPT) PARMS((#DHEX64 *RECEIVED) (#DVAL32 *RETURNED))
* Key to be used for the decryption. This must be the
* same key that was used for the encryption.
CHANGE FIELD(#DLEN) TO(32)
* convert input hex value to character
* USE BUILTIN(HEXTOBIN) WITH_ARGS(#DHEX64)
* TO_GET(#DENC32)
* Use DECRYPT BIF to decrypt character #DENC16 of length
* #DLEN using #DKEY16 to return decrypted value,#DVAL16
USE BUILTIN(DECRYPT) WITH_ARGS(#DHEX64 #DLEN #DKEY16 YES) TO_GET(#DVAL32)
ENDROUTINE