AESEncoder()

Syntax

Result = AESEncoder(*Input, *Output, Size, *Key, Bits, *InitializationVector [, Mode])
Description
Encodes the specified input buffer using the AES algorithm into the output buffer.

Parameters

*Input The input buffer with the plain data.
*Output The output buffer which will receive the encoded data. It has to be different than the input buffer.
Size The amount of bytes to encode. It has to be at least 16 bytes. To encode something smaller, padding has to be added before the encoding.
*Key A buffer containing the key for encoding. Its size depends of the 'Bits' parameter: 16 bytes for 128-bit encryption, 24 bytes for 192 bit and 32 bytes for 256-bit.
Bits The size of the key used by the ciphering. Valid values are 128, 192 and 256.
*InitializationVector The InitializationVector is a random data block, used to initialize the ciphering to avoid breach in decoding (only needed when using the #PB_Cipher_CBC mode). The initialization vector is always 16 bytes long.
Mode (optional) This can be one of the following value:
  #PB_Cipher_CBC: Default mode of encoding (Cipher Block Chaining). Needs an '*InitializationVector'.
                  Recommended as more secure than ECB mode.
  #PB_Cipher_ECB: Alternative mode (Electronic CodeBook). It doesn't uses random value nor chaining 
                  (each block is ciphered independently) making it very weak compared to CBC, and shouldn't be used for
                  serious ciphering.

Return value

Returns nonzero if the encoding was successful, zero otherwise.

Remarks

AES is an industry class cipher algorithm and is good balanced between speed and security. Here is the Wikipedia introduction about AES: 'In cryptography, the Advanced Encryption Standard (AES) is an encryption standard adopted by the U.S. government. The standard comprises three block ciphers, AES-128, AES-192 and AES-256, adopted from a larger collection originally published as Rijndael. Each AES cipher has a 128-bit block size, with key sizes of 128, 192 and 256-bit, respectively. The AES ciphers have been analyzed extensively and are now used worldwide.'

PureBasic uses a RFC compliant implementation of AES. More information can be found in the RFC 3602: http://www.ietf.org/rfc/rfc3602.txt.

Example: CBC

  ; Encrypt some string
  ;
  String$ = "Hello this is a test for AES"
  
  StringMemorySize = StringByteLength(String$) + SizeOf(Character) ; Space for the string and its null terminating character
  *CipheredString = AllocateMemory(StringMemorySize)   
  *DecipheredString = AllocateMemory(StringMemorySize) 
  
  If AESEncoder(@String$, *CipheredString, StringByteLength(String$), ?Key, 128, ?InitializationVector)
    Debug "Ciphered: "+PeekS(*CipheredString) ; warning, it will stop on the first null byte, only for demo purpose
    
    AESDecoder(*CipheredString, *DecipheredString, StringByteLength(String$), ?Key, 128, ?InitializationVector)
    Debug "Deciphered: "+PeekS(*DecipheredString)
  EndIf

  DataSection
    Key:
      Data.b $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06
  
    InitializationVector:
      Data.b $3d, $af, $ba, $42, $9d, $9e, $b4, $30, $b4, $22, $da, $80, $2c, $9f, $ac, $41
  EndDataSection
  

Example: ECB

  ; Should be compiled in ascii mode
  ;
  String$ = "Hello this is a test for AES"
  
  *CipheredString   = AllocateMemory(Len(String$)+ SizeOf(Caractere)) ; Space for the string and its
  *DecipheredString = AllocateMemory(Len(String$)+ SizeOf(Caractere)) ; null terminating character (ASCII mode)
  
  If AESEncoder(@String$, *CipheredString, Len(String$), ?Key, 128, 0, #PB_Cipher_ECB)
    Debug "Ciphered: "+PeekS(*CipheredString)
    
    AESDecoder(*CipheredString, *DecipheredString, Len(String$), ?Key, 128, 0, #PB_Cipher_ECB)
    Debug "Deciphered: "+PeekS(*DecipheredString)
  EndIf

  DataSection
    Key:
      Data.b $06, $a9, $21, $40, $36, $b8, $a1, $5b, $51, $2e, $03, $d5, $34, $12, $00, $06
  EndDataSection
  

See Also

AESDecoder(), StartAESCipher()

Supported OS

All

<- AESDecoder() - Cipher Index - AddCipherBuffer() ->