ENCRYPT() - Encrypt Specified String

Syntax in BBj 9.0 and higher


Syntax prior to BBj 9.0



In PRO/5 5.0 and BBj 4.0 and higher, the ENCRYPT() encrypts the specified string and returns an encrypted string. Note that the encrypted string may be longer than the specified string.






The encryption key. BASIS recommends a password of at least 20 characters when using AES-128 and at least 40 characters when using AES-256.




Uses AES-128 bit encryption, which is also the default.

In BBj 6.0 and higher, and PRO/5 6.0 and higher, uses AES-256 bit encryption.



In BBj 16.0 and higher, specifies the character set encoding of the password string.

To utilize AES-256 in BBj, the Java Cryptography Extension (JCE) module must be installed. These files are available for download at Oracle's Java SE Downloads page under "Additional Resources" near the bottom of the page.




The text that the ENCRYPT function will cipher.


The encryption key. BASIS recommends a password of at least 20 characters when using AES-128 and at least 40 characters when using AES-256.

If both the password parameter and the CRYPTPASS= mode are specified, the password value takes precedence.

BBj 16.0 and Higher

Example 1

pass$ = $F30A68E0CB50A16BC80B4A9D7B934986B58AC357601E164B8B$

pass$ = pass$ + $8B0DC4C6E34EB3AF11173DCD1B299B9CA60341E8BF53415ED7$

text$ = "abc123"

print "encrypt ",text$

print hta(encrypt(text$,pass$))

print hta(encrypt(text$,pass$,mode="charset=windows-1252"))

print "decrypt ",text$

crypt$ = encrypt(text$,pass$)

print decrypt(crypt$,pass$)

crypt$ = encrypt(text$,pass$,mode="charset=windows-1252")

print decrypt(crypt$,pass$,mode="charset=windows-1252")

BBj 9.0 and Higher

Example 2

LET C$=ENCRYPT(A$,"password")

Prior to BBj 9.0

Example 3

LET C$=ENCRYPT(A$,MODE="CRYPTPASS=use at least 20 characters")

Example 4


Example 5

10 REM Determine resultant length of encrypted 16-digit credit card field
20 REM since it may be longer than the unencrypted version. This requires
30 REM more space to be set aside for the field definition in the data file.
40 encrypted_card$ = encrypt(fill(16,"0"),MODE="CRYPTPASS=SamplePassword")
50 ecrypted_len = len(encrypted_card$)
60 REM Create two string templates - one for the unencrypted version which is used
70 REM by the program and one for the file which holds the encrypted version
80 DIM decrypted_credit$:"custnum:C(8),creditcard:C(16)"
90 DIM encrypted_credit$:"custnum:C(8),creditcard:O(" + str(ecrypted_len) + ")"

100 REM Create sample data file
110 MKEYED "credit",[1:1:8],[1:9:ecrypted_len],0,ecrypted_len + 8
120 CHAN=UNT; OPEN(CHAN) "credit"
130 FOR I = 1 to 5
140 REM Get sample data into the unencrypted template
150 DREAD decrypted_credit.custnum$,decrypted_credit.creditcard$
160 REM Encrypt the credit card number and fill out the encrypted template
170 encrypted_credit.creditcard$ = ENCRYPT(decrypted_credit.creditcard$,MODE="CRYPTPASS=SamplePassword")
180 encrypted_credit.custnum$ = decrypted_credit.custnum$
190 REM Write out the data to the file
200 WRITERECORD(CHAN)encrypted_credit$
210 NEXT I

300 PRINT "Read in the first record from the file"
310 READRECORD(CHAN,key="00000001")encrypted_credit$
320 PRINT " Credit card number in the file: ", encrypted_credit.creditcard$
330 decrypted_credit.creditcard$ = DECRYPT(encrypted_credit.creditcard$,MODE="CRYPTPASS=SamplePassword")
340 PRINT " Decrypted credit card number: ", decrypted_credit.creditcard$

400 PRINT "Read a record by specifying an encrypted field as the key"
410 REM First, figure out the encrypted version of the credit card, then specify it as a KEY on the READ
420 encrypted_credit.creditcard$ = ENCRYPT("2222333344445555",MODE="CRYPTPASS=SamplePassword")
430 PRINT " Credit card number used for the KEY: ", encrypted_credit.creditcard$
440 READRECORD(CHAN,knum=1,key=encrypted_credit.creditcard$)encrypted_credit$
450 PRINT " Credit card number in the file: ", encrypted_credit.creditcard$
460 decrypted_credit.creditcard$ = DECRYPT(encrypted_credit.creditcard$,MODE="CRYPTPASS=SamplePassword")
470 PRINT " Decrypted credit card number: ", decrypted_credit.creditcard$

500 END

5000 DATA "00000001","0000111122223333"
5010 DATA "00000002","1111222233334444"
5020 DATA "00000003","2222333344445555"
5030 DATA "00000004","3333444455556666"
5040 DATA "00000005","4444555566667777"

See Also


Functions - Alphabetical Listing