Advanced Encryption Standard
From Crypto++ Wiki
Contents |
Usage
Default Key Length
The default is specified by AES::DEFAULT_KEYLENGTH.
Block Size
The block size is determined by AES::BLOCKSIZE.
Reference to block cipher object
You can also create a mode object that holds a reference to a block cipher object rather than an instance of it:
AES::Encryption aesEncryption(key, AES::DEFAULT_KEYLENGTH); CFB_Mode_ExternalCipher::Encryption cfbEncryption(aesEncryption, iv);
ECB and CBC mode remarks
For ECB and CBC mode, you must process data in multiples of the block size. Alternatively, you can wrap StreamTransformationFilter around the mode object and use it as a Filter object. The StreamTransformationFilter will buffer data into blocks as required.
CFB_MODE< AES > cfbEncryptor; ... std::string ciphertext; StreamTransformationFilter cfbEncryptor(cfbEncryption, new StringSink(ciphertext)); cfbEncryptor.Put(plaintext, 100); // Input more plaintext as needed ... cfbEncryptor.MessageEnd(); return ciphertext;
Sample Programs
Encrypting and Decrypting Using AES
This example uses in-place encryption and decryption where the input and output buffers are identical:
AutoSeededRandomPool rnd; // Generate a random key byte key[AES::DEFAULT_KEYLENGTH]; rnd.GenerateBlock(key, AES::DEFAULT_KEYLENGTH); // Generate a random IV byte iv[AES::BLOCKSIZE]; rnd.GenerateBlock(iv, AES::BLOCKSIZE); char plainText[] = "Hello! How are you."; int messageLen = (int)strlen(plainText) + 1; ////////////////////////////////////////////////////////////////////////// // Encrypt CFB_Mode<AES>::Encryption cfbEncryption(key, sizeof(key), iv); cfbEncryption.ProcessData((byte*)plainText, (byte*)plainText, messageLen); ////////////////////////////////////////////////////////////////////////// // Decrypt CFB_Mode<AES>::Decryption cfbDecryption(key, sizeof(key), iv); cfbDecryption.ProcessData((byte*)plainText, (byte*)plainText, messageLen);
Generating an AES Key from a Diffie-Hellman Session Key
See Using Diffie-Hellman to generate an AES key.
Encrypting a string using AES
byte key[AES::DEFAULT_KEYLENGTH], iv[AES::BLOCKSIZE]; string plainText; // ... populate key, iv, plainText here string cipher; StringSink* sink = new StringSink(cipher); Base64Encoder* base64_enc = new Base64Encoder(sink); CBC_Mode<AES>::Encryption aes(key, sizeof(key), iv); StreamTransformationFilter* aes_enc = new StreamTransformationFilter(aes, base64_enc); StringSource source(plainText, true, aes_enc);
Downloads
AES-GCM-Test.zip - AES in GCM mode (confidentiality and authentication) - 7KB
AES-CCM-Test.zip - AES in CCM mode (confidentiality and authentication) - 8KB
AES-CBC-Test.zip - AES in CBC mode (confidentiality only) - 5KB
Categories: Sample | Download | Symmetric Cipher | Block Cipher | FIPS | NESSIE | ISO
