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. 'Bits' specify the size of the key used by the ciphering. Valid values are 128, 192 and 256. 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). Its size depends of the 'Bits' parameter: l6 bytes for 128 bit encryption, 24 bytes for 196 bit and 32 bytes for 256 bit.
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.
The optional 'Mode' parameter 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 less strong the CBC.Example: CBC
; Should be compiled in ascii mode ; String$ = "Hello this is a test for AES" *CipheredString = AllocateMemory(Len(String$)+1) ; Space for the string and its *DecipheredString = AllocateMemory(Len(String$)+1) ; null terminating character (ASCII mode) If AESEncoder(@String$, *CipheredString, Len(String$), ?Key, 128, ?InitializationVector) Debug "Ciphered: "+PeekS(*CipheredString) AESDecoder(*CipheredString, *DecipheredString, Len(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 EndDataSectionExample: ECB
; Should be compiled in ascii mode ; String$ = "Hello this is a test for AES" *CipheredString = AllocateMemory(Len(String$)+1) ; Space for the string and its *DecipheredString = AllocateMemory(Len(String$)+1) ; 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
Supported OS
All