Class CryptTable() Professional

Class function of the CryptTable() class

Description

The CryptTable class manages encryption and decryption of database tables. A database table is encrypted by specifying the encryption type and which key to use. The key must be retained in a safe place and is required to open the table after closing. It is also required to use the same CryptProvider, of course.

Data is encrypted record-wise using the operation mode CBC/CTS by default. This mode requires an additional field to store the initialization vector (IV), which is a prerequisite for CBC. The IV is filled with random data, and serves as the first block to be XOR'ed with the following data block. This enhances the level of security for records containing data with minor differences, such as customer numbers, timestamp fields etc. It also prevents the record from being falsified by exchanging a block.

The method :setupStruct()will adapt a given table structure to comply with the encryption mode. If the length of the record is less than 1 block length, it will be extended.

SecureKey():destroy() can be called right after passing the key object.

Class methods
:new()
Creates an instance of the CryptTable class.
Instance variables
:ivFieldName
Name of initialization vector field.
Methods
:enable()
Enables automatic encryption/decryption of a table, opened in a workarea.
:disable()
Disables automatic encryption/decryption of the attached workarea.
:setupStruct()
Set up a table structure to comply with the choosen encryption operation mode.
Examples
Encrypting an uncrypted table
// Open a table and encrypt data 

 #include "crypt.ch" 

 PROCEDURE MAIN(cKey) 
    LOCAL oKey 
    LOCAL oCryptedDbf 
    LOCAL aTarStruct 

    // Load FOXCDX DatabaseEngine as it is required 
    DbeLoad("FOXDBE") 
    DbeLoad("CDXDBE") 
    DbeBuild("FOXCDX","FOXDBE","CDXDBE") 
    
    // use default key 
    IF(cKey==NIL) 
      cKey :=  "A5:25:11:DA:1C:5C:72:9A:C9:78:03:FD:65:DF:0A:32" 
    ENDIF 

    // setup secure key object 
    oKey := SecureKey():new() 
    oKey:setStrKey(cKey) 

    // create a CryptTable()-instance 
    oCryptedDbf := CryptTable():new(,oKey) 

    // destroy key instance 
    oKey:destroy() 

    // open source table 
    USE customer NEW VIA DBFNTX 
    aTarStruct := oCryptedDbf:setupStruct(,"FOXCDX") 

    // create and open new table 
    DbCreate("crypted", aTarStruct) 
    USE crypted NEW ALIAS secure 

    // enable automatic encrypting/decrypting for workarea 
    oCryptedDbf:enable() 

    DO WHILE !customer->(eof()) 
        secure->(DbAppend()) 
        FOR i:= 1 TO customer->(Fcount()) 
            secure->(FieldPut(oCryptedDbf:fieldOffset+i, customer->(FieldGet(i)))) 
        NEXT 
        customer->(dbskip()) 
    ENDDO 
    // disable automatic encrypting/decrypting 
    oCryptedDbf:disable() 
    CLOSE ALL 

 RETURN 
Open a encrypted table
// Open a encrypted table using a given key 

 #include "crypt.ch" 

 PROCEDURE MAIN(cKey) 
    LOCAL oKey 
    LOCAL oCryptedDbf 
    LOCAL aTarStruct 

    // Load FOXCDX DatabaseEngine as it is required 
    DbeLoad("FOXDBE") 
    DbeLoad("CDXDBE") 
    DbeBuild("FOXCDX","FOXDBE","CDXDBE") 

    // use default key 
    IF(cKey==NIL) 
      cKey :=  "A5:25:11:DA:1C:5C:72:9A:C9:78:03:FD:65:DF:0A:32" 
    ENDIF 

    oKey := SecureKey():new() 
    oKey:setStrKey(cKey) 

    // create a CryptTable()-instance 
    oCryptedDbf := CryptTable():new(,oKey) 

    // destroy key instance 
    oKey:destroy() 

    // crypted tables are automatically detected 
    USE crypted NEW ALIAS secure 
    oCryptedDbf:enable() 
    Browse() 
    CLOSE ALL 
 RETURN 
Filling a new table using encryption
// In this example, a new table is filled with data 
// using encryption 

 #include "crypt.ch" 

 PROCEDURE MAIN() 
    LOCAL oKey 
    LOCAL oCryptedDbf 
    LOCAL aTarStruct 

    oKey := SecureKey():generate() 
    ? "Key:", oKey:toString(,":") 

    // create a CryptTable()-instance 
    oCryptedDbf := CryptTable():new(,oKey) 

    // destroy key instance 
    oKey:destroy() 

    aTarStruct := oCryptedDbf:setupStruct({{"ID", "C", 4, 0}, {"NAME", "C", 20, 0}} 
                                          ,"FOXCDX") 

    DbCreate("crypted", aTarStruct) 
    USE crypted NEW ALIAS secure 

    // enable automatic encrypting/decrypting 
    oCryptedDbf:enable() 

    secure->(DbAppend()) 
    secure->id   := "0001" 
    secure->name := "Valentina Titowa" 
    secure->(DbAppend()) 
    secure->id   := "0002" 
    secure->name := "Natalya Sayko" 
    secure->(DbAppend()) 
    secure->id   := "0003" 
    secure->name := "Leonid Menaker" 

    // disable automatic encrypting/decrypting 
    oCryptedDbf:disable() 
    CLOSE ALL 

 RETURN 

 // Load FOXCDX DatabaseEngine as it is required 
 PROCEDURE DBESYS 
   DbeLoad("FOXDBE") 
   DbeLoad("CDXDBE") 
   DbeBuild("FOXCDX","FOXDBE","CDXDBE") 
 RETURN 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.