IMPORTANT:
The functions within this FLL have changed. Please refer to the latest
documention for the VFP Encryption FLL that can be found at the
following link:

Major VFP Encryption Update



The free vfpencryption71.fll has been updated. Changes include:

  • Bug fix for a very obscure problem when encrypting and decrypting Jpg image files using AES 256
  • Added a CRC() and CRCFile() function that produces 16 and 32 bit CRCs for Strings and Files
  • Added HashRecord() and CRCRecord() functions that allow a VFP developer to quickly and easily generate Hashes or CRCs for the current record in an opened table

Download the Latest Version of the VFP Encryption 71 FLL (63 KB approx.)


 vfpencryption.fll Documentation…


Function ENCRYPT()

Signature: Encrypt(cStringtoEncrypt, cSecretKey[, nEncryptionType[, nEncryptionMode]])

Parameters:

cStringtoEncrypt – A plain text string that you want to have encrypted, such as “Hello World!”

cSecretKey – A plain text string that is the Key you want used during encryption, such as “My_SeCrEt_KeY”.
Please note that keys may need to be of a particular length for certain types of encryption. Refer below for more information.

nEncryptionType – There are currently 5 types of encryption available. The value of this parameter determines that type of encryption used and how long your Secret Key should be. A single character in Visual FoxPro is  equal to 1 byte or 8 bits. So an encryption algorithm requiring a 128-bit key would need a Secret Key of 16 characters (16 x 8 = 128).

   0 = AES128 (requires a 16 character Key)
   1 = AES192 (requires a 24 character Key)
   2 = AES256 (requires a 32 character Key) *Default
   4 = Blowfish (requires a 56 character Key)
   8 = TEA (requires a 16 character Key)
   1024 = RC4 (Key can be any length)

nEncryptionMode – There are three different modes available for the each of the encryption types listed above. They include: Electronic Code Book (ECB), Cipher Block Chaining (CBC) and Cipher Feedback Block (CFB). The nEncryptionMode parameter does not apply to RC4 encryption (nEncryptionType = 1024).

   0 = ECB *Default
   1 = CBC
   2 = CFB

Return Value:

Character data type – the encrypted form of cStringtoEncrypt.

Remarks:

When saving the return value of Encrypt() function to a field in a table, remember that Visual FoxPro will append blanks to the end of the string in order to fill the character field to its designated length. This can cause problems when decrypting the data as the spaces will be considered part of the encrypted string. To work around this, I suggest placing a single CHR(0) at the end of the encrypted string when saving it to the table. Then when decrypting the data just the portion prior to the CHR(0) can be sent into the Decrypt() function. This does not apply when using RC4 encryption (nEncryptionType = 1024).


Function DECRYPT()

Signature: Decrypt(cEncryptString, cSecretKey[, nDecryptionType[, nDecryptionMode]])

Parameters:

cEncryptedString – A string that has been encrypted using the Encrypt() function.

cSecretKey – A plain text string that is the same Key that you used when you encrypted the data using the Encrypt function, such as “My_SeCrEt_KeY”.
Please note that keys may need to be of a particular length for certain types of decryption. Refer below for more information.

nDecryptionType – There are currently 5 types of decryption available and they correspond to the same ones available in Encrypt(). A single character in Visual FoxPro is  equal to 1 byte or 8 bits. So an decryption algorithm requiring a 128-bit key would need a Secret Key of 16 characters (16 x 8 = 128).

   0 = AES128 (requires a 16 character Key)
   1 = AES192 (requires a 24 character Key)
   2 = AES256 (requires a 32 character Key) *Default
   4 = Blowfish (requires a 56 character Key)
   8 = TEA (requires a 16 character Key)
   1024 = RC4 (Key can be any length)

nDecryptionMode – There are three different modes available for the each of the encryption types listed above. They include: Electronic Code Book (ECB), Cipher Block Chaining (CBC) and Cipher Feedback Block (CFB). The nDecryptionMode parameter does not apply to RC4 decryption (nDecryptionType = 1024).

   0 = ECB *Default
   1 = CBC
   2 = CFB

Return Value:

Character data type – the decrypted form of cEncryptedString followed by a variable number of CHR(0)s. See Remarks below for further clarification

Remarks:

IMPORTANT: Decryption is done on blocks of memory, so when the decrypt function returns the encrypted string it will be followed by a variable number of CHR(0)s unless the decrypted string just happens to end at exactly the same location as the last block decrypted. These extraneous CHR(0)’s can be removed using a number of Visual FoxPro functions, such as STRTRAN(), CHRTRAN(), or a combination of LEFT() and AT(). This does not apply when using RC4 decryption (nDecryptionType = 1024).


Function ENCRYPTFILE()

Signature: EncryptFile(cFiletoEncrypt, cDestinationFile, cSecretKey[, nEncryptionType[, nEncryptionMode]])

Parameters:

cFiletoEncrypt – A plain text string that is the fullpath to the file you wish to be encrypted, such as “C:\SensitiveInfo.doc”

cDestinationFile – A plain text string that is the fullpath to an encrypted file you wish to have created on disk, such as “C:\EncryptedInfo.doc”. If this file doesn’t exist then it will be created for you.

cSecretKey – A plain text string that is the Key you want used during encryption, such as “My_SeCrEt_KeY”.
Please note that keys may need to be of a particular length for certain types of encryption. Refer below for more information.

nEncryptionType – There are currently 5 types of encryption available. The value of this parameter determines that type of encryption used and how long your Secret Key should be. A single character in Visual FoxPro is  equal to 1 byte or 8 bits. So an encryption algorithm requiring a 128-bit key would need a Secret Key of 16 characters (16 x 8 = 128).

   0 = AES128 (requires a 16 character Key)
   1 = AES192 (requires a 24 character Key)
   2 = AES256 (requires a 32 character Key) *Default
   4 = Blowfish (requires a 56 character Key)
   8 = TEA (requires a 16 character Key)
   1024 = RC4 (Key can be any length)

nEncryptionMode – There are three different modes available for the each of the encryption types listed above. They include: Electronic Code Book (ECB), Cipher Block Chaining (CBC) and Cipher Feedback Block (CFB). This does not apply when using RC4 encryption (nEncryptionType = 1024).

   0 = ECB *Default
   1 = CBC
   2 = CFB

Return Value:

None

Remarks:

Currently the cFiletoEncrypt and cDestinationFile parameters cannot point to the same file. This may be revised in a future version. But for safety sake, this function requires that the original file be left untouched.


Function DECRYPTFILE()

Signature: DecryptFile(cEncryptedFile, cDestinationFile, cSecretKey[, nDecryptionType[, nDecryptionMode]])

Parameters:

cEncyptedFile – A plain text string that is the fullpath to the file you wish to be decrypted, such as “C:\EncryptedInfo.doc”

cDestinationFile – A plain text string that is the fullpath to a decrypted file you wish to have created on disk, such as “C:\SensitiveInfo.doc”. If this file doesn’t exist then it will be created for you.

cSecretKey – A plain text string that is the same Key that you used when you encrypted the data using the Encrypt function, such as “My_SeCrEt_KeY”.
Please note that keys may need to be of a particular length for certain types of decryption. Refer below for more information.

nDecryptionType – There are currently 5 types of decryption available and they correspond to the same ones available in Encrypt(). A single character in Visual FoxPro is  equal to 1 byte or 8 bits. So an decryption algorithm requiring a 128-bit key would need a Secret Key of 16 characters (16 x 8 = 128).

   0 = AES128 (requires a 16 character Key)
   1 = AES192 (requires a 24 character Key)
   2 = AES256 (requires a 32 character Key) *Default
   4 = Blowfish (requires a 56 character Key)
   8 = TEA (requires a 16 character Key)
   1024 = RC4 (Key can be any length)

nDecryptionMode – There are three different modes available for the each of the encryption types listed above. They include: Electronic Code Book (ECB), Cipher Block Chaining (CBC) and Cipher Feedback Block (CFB). This does not apply when using RC4 decryption (nDecryptionType = 1024).

   0 = ECB *Default
   1 = CBC
   2 = CFB

Return Value:

None

Remarks:

As with EncryptFile(), the cFiletoEncrypt and cDestinationFile parameters cannot
point to the same file.


Function HASH()

Signature: Hash(cStringtoHash[, nHashType])

Parameters:

cStringtoHash – A plain text string you wish to have hashed

nHashType – The type of hash function to generate. There are currently 7 different hash functions supported

1 = SHA1 (a.k.a SHA160)
2 = SHA256
3 = SHA384
4 = SHA512 *Default
5 = MD5
6 = RIPEMD128
7 = RIPEMD256

Return Value:

Binary Character Data – the hash for cStringtoHash.

Remarks:

The hash is returned as a series of binary characters. However, it is more common to see hashes in a hexBinary format. This can be accomplished in Visual FoxPro by taking the return of the Hash() function and sending it in as a parameter to the STRCONV() function. For example:

?STRCONV(Hash(“Some String”), 15) && hexBinary Hash


Function HASHFILE()

Signature: HashFile(cFileName[, nHashType])

Parameters:

cFileName – The fullpath and name of an existing file you wish to generate a message digest for

nHashType – The type of hash function to generate. There are currently 7 different hash functions supported

1 = SHA1 (a.k.a SHA160)
2 = SHA256
3 = SHA384
4 = SHA512 *Default
5 = MD5
6 = RIPEMD128
7 = RIPEMD256

Return Value:

Binary Character Data – the hash for cFileName.

Remarks:

The hash is returned as a series of binary characters. However, it
is more common to see hashes in a hexBinary format. This can be accomplished
in Visual FoxPro by taking the return of the HashFile() function and sending
it in as a parameter to the STRCONV() function. For example:

?STRCONV(HashFile(“C:\MyFile.txt”), 15) && hexBinary Hash


Function HASHRECORD()

Signature: HashRecord(cAlias[, nHashType[,lIncludeMemos]])

Parameters:

cAlias – The table
alias containing the record to be hashed

nHashType – The type of hash function to generate.
There are currently 7 different hash functions supported

1 = SHA1 (a.k.a SHA160)
2 = SHA256
3 = SHA384
4 = SHA512 *Default
5 = MD5
6 = RIPEMD128
7 = RIPEMD256

lIncludeMemos – Flag determining
whether Memo fields should be included when generating the message digest. .T.
= Include Memo Fields, .F. = Exclude Memo Fields

Return Value:

Binary Character Data – the hash for the current record in cAlias.

Remarks:

The hash is returned as a series of binary characters. However, it
is more common to see hashes in a hexBinary format. This can be accomplished
in Visual FoxPro by taking the return of the HashRecord() function and sending
it in as a parameter to the STRCONV() function. For example:

?STRCONV(HashRecord(“MyTable”,5,.T.), 15) && hexBinary Hash


Function CRC()

Signature: CRC(cExpression[, nCRCType])

Parameters:

cExpression – The string
you wish to have a CRC generated for

nCRCType – The type of CRC to generate. There are
currently 2 different CRC types supported

1 = 16-bit
2 = 32-bit

Return Value:

Numeric Data – the CRC for cExpression.

Remarks:

The CRC that is returned is unsigned, which means that the returned 16-bit
CRC needs to be treated as a 4 Byte numeric value and the 32-bit CRC as a 8
byte numeric value in Visual FoxPro. The operation of the CRC() function presented
here is quite similar to Visual FoxPro’s Sys(2007) function, however you will
find that creation of 32-bit CRCs is much faster using this function.


Function CRCFILE()

Signature: CRCFile(cFileName[, nCRCType])

Parameters:

cFileName – The fullpath
and name of an existing file you wish to generate a CRC for

nCRCType – The type of CRC to generate. There are
currently 2 different CRC types supported

1 = 16-bit
2 = 32-bit

Return Value:

Numeric Data – the CRC for cFileName.

Remarks:

The CRC that is returned is unsigned, which means that the returned 16-bit
CRC needs to be treated as a 4 Byte numeric value and the 32-bit CRC as a 8
byte numeric value in Visual FoxPro.


Function CRCRECORD()

Signature: CRChRecord(cAlias[, nCRCType[,lIncludeMemos]])

Parameters:

cAlias – The table
alias containing the record to be hashed

nCRCType – The type of CRC to generate. There are
currently 2 different CRC types supported

1 = 16-bit
2 = 32-bit

lIncludeMemos – Flag determining
whether Memo fields should be included when generating the message digest. .T.
= Include Memo Fields, .F. = Exclude Memo Fields

Return Value:

Numeric Data – the CRC for the current record in cAlias .

Remarks:

The CRC that is returned is unsigned, which means that the returned 16-bit
CRC needs to be treated as a 4 Byte numeric value and the 32-bit CRC as a 8
byte numeric value in Visual FoxPro. The operation of the CRC() function presented
here is quite similar to Visual FoxPro’s Sys(2017) function, however you will
find that this CRC function is faster than Visual FoxPro’s Sys(2017). Also,
this function allows you to specify a table alias, which allows CRCs to be created
for a record in a table other than the currently selected work area. On the
downside, this function does not allow you to specify a comma delimited list
of fields to exclude like Sys(2017) does.