AES Manual
An Advanced Encryption Standard (AES, also known as Rijndael) class to encrypt/decrypt data.
// Some plain text to encrypt. const Char plainText[] = "O brave new world / That has such people in't!" ; const Int32 length = sizeof (plainText);
// Declare secret key and encryption block size. const Int32 blockSize = 256; // in bits (either 128, 192 or 256) const Char key[32] = { '7' , 'c' , '3' , '0' , 'e' , '0' , '0' , 'b' , 'b' , '6' , '2' , '7' , '1' , '4' , '9' , '5' }; // either 16, 24 or 32 bytes const Int32 keyLength = sizeof (key) * 8; // in bits (either 128, 192 or 256) DebugAssert ((keyLength == 128) || (keyLength == 192) || (keyLength == 256));
// Memory to store the encrypted data. void * buffer = nullptr ; Int encryptedSize = 0;
// Encrypt data. { // Get size of the memory needed to store the encrypted data. encryptedSize = AES::CalcEncryptedDataSize (blockSize, length); DebugAssert (encryptedSize >= length); // Note: encrytedSize is always equal or larger than unencrypted size.
// Allocate needed memory. buffer = NewMem ( char , encryptedSize) iferr_return ; // No need to clear memory here, will be filled with random data next.
// Fill buffer with random content for increased security (the buffer part behind plainText is also input to the encryption). maxon::Block<UChar> memBlock(( UChar *)buffer, encryptedSize); maxon::SecureRandomProvider srp = maxon::SecureRandom::GetDefaultProvider (); if (! maxon::SecureRandom::GetRandomNumber (srp, memBlock)) return maxon::UnexpectedError( MAXON_SOURCE_LOCATION );
// Copy the given text into the memory. CopyMem (plainText, buffer, length);
// Decrypt data again. { AutoAlloc<AES> aes; if (buffer && aes && aes-> Init (blockSize, keyLength)) { if (aes-> Decrypt (buffer, encryptedSize, key)) { const maxon::String text { ( char *)buffer }; ApplicationOutput ( "Plain Text: @" , text); } } } DeleteMem (buffer);
AES objects are created with the usual tools, see Entity Creation and Destruction Manual (Classic) .