Class AesCrypt() Professional

Class function of the AesCrypt class

Description

The AesCrypt class provides the interface for AES encryption/decryption. Its role is to provide a cryptographic implementation of AES, the Advanced Encryption Standard as announced by NIST, effective May 26 2002. Detailed information about AES encryption can be found in the corresponding Wikipedia article under https://en.wikipedia.org/wiki/Advanced_Encryption_Standard

The class AesCrypt is derived from the class CryptProvider. This class is not yet documented because it is an abstract, which cannot be instantiated.

AES uses the Rijndael algorithm to encrypt/decrypt data. It is a block code where the block size is always 128 bit. The key length can be 128, 192 or 256 bit.

The AesCrypt() class supports AES encryption in the ECB (electronic code book), CBC (cipher block chaining) and CTS (CBC with cipher text stealing) modes.

In general, in ECB and CBC mode, the length of the message to be encrypted must be a multiple of the block length. In CTS mode the length of the message must be at least 2 block. In all modes, the :prepareBuffer() method can be used to extend the message to the correct length.

In ECB mode the encrypted message has the same length as the plaintext message. In CBC and CTS modes, encryption requires an initialization vector that becomes part of the encrypted message. Thus, in these modes (CBC and CTS), the encrypted message is longer than the original message by the IV. For more information, see the documentation for the :new() method.

SecureKey():destroy() can be called right after setting the key object, either after :new() or :setKey().

Class methods
:new()
Creates an instance of the AesCrypt class.
Methods
:encrypt()
Encrypts a buffer using a key.
:decrypt()
Decrypts a previously encrypted buffer using a key.
:prepareBuffer()
Prepares a buffer for encryption.
:setKey()
Provides a key used for encryption or decryption.
Examples
AesCrypt handling
// In this example, AesCrypt() is used 
// to encrypt a single buffer 

#include "crypt.ch" 

PROCEDURE MAIN(cBuffer) 
   LOCAL oAes 
   LOCAL oKey 
   LOCAL cResult 

   // generate a key and print out 
   oKey := SecureKey():generate() 
   ? oKey:toString() 

   // create AesCrypt()-instance using the key 
   oAes := AesCrypt():new(oKey) 

   // destroy key value 
   oKey:destroy() 

   // prepare input buffer length 
   oAes:prepareBuffer(@cBuffer)       
   
   // encrypt buffer 
   cResult := oAes:encrypt(cBuffer) 
   ? cResult 
   
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.