70

I have been searching the Internet for good c++ AES code sample/tutorial that teaches the basics of the encryption technology and the use of the Library but so far I have had no luck getting decent material.

good: Easy to understand (Just the basics for on the go study).

Ðаn
  • 10,934
  • 11
  • 59
  • 95
yohannist
  • 4,166
  • 3
  • 35
  • 58

1 Answers1

99

Official document of Crypto++ AES is a good start. And from my archive, a basic implementation of AES is as follows:

Please refer here with more explanation, I recommend you first understand the algorithm and then try to understand each line step by step.

#include <iostream>
#include <iomanip>

#include "modes.h"
#include "aes.h"
#include "filters.h"

int main(int argc, char* argv[]) {

    //Key and IV setup
    //AES encryption uses a secret key of a variable length (128-bit, 196-bit or 256-   
    //bit). This key is secretly exchanged between two parties before communication   
    //begins. DEFAULT_KEYLENGTH= 16 bytes
    CryptoPP::byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ], iv[ CryptoPP::AES::BLOCKSIZE ];
    memset( key, 0x00, CryptoPP::AES::DEFAULT_KEYLENGTH );
    memset( iv, 0x00, CryptoPP::AES::BLOCKSIZE );

    //
    // String and Sink setup
    //
    std::string plaintext = "Now is the time for all good men to come to the aide...";
    std::string ciphertext;
    std::string decryptedtext;

    //
    // Dump Plain Text
    //
    std::cout << "Plain Text (" << plaintext.size() << " bytes)" << std::endl;
    std::cout << plaintext;
    std::cout << std::endl << std::endl;

    //
    // Create Cipher Text
    //
    CryptoPP::AES::Encryption aesEncryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Encryption cbcEncryption( aesEncryption, iv );

    CryptoPP::StreamTransformationFilter stfEncryptor(cbcEncryption, new CryptoPP::StringSink( ciphertext ) );
    stfEncryptor.Put( reinterpret_cast<const unsigned char*>( plaintext.c_str() ), plaintext.length() );
    stfEncryptor.MessageEnd();

    //
    // Dump Cipher Text
    //
    std::cout << "Cipher Text (" << ciphertext.size() << " bytes)" << std::endl;

    for( int i = 0; i < ciphertext.size(); i++ ) {

        std::cout << "0x" << std::hex << (0xFF & static_cast<CryptoPP::byte>(ciphertext[i])) << " ";
    }

    std::cout << std::endl << std::endl;

    //
    // Decrypt
    //
    CryptoPP::AES::Decryption aesDecryption(key, CryptoPP::AES::DEFAULT_KEYLENGTH);
    CryptoPP::CBC_Mode_ExternalCipher::Decryption cbcDecryption( aesDecryption, iv );

    CryptoPP::StreamTransformationFilter stfDecryptor(cbcDecryption, new CryptoPP::StringSink( decryptedtext ) );
    stfDecryptor.Put( reinterpret_cast<const unsigned char*>( ciphertext.c_str() ), ciphertext.size() );
    stfDecryptor.MessageEnd();

    //
    // Dump Decrypted Text
    //
    std::cout << "Decrypted Text: " << std::endl;
    std::cout << decryptedtext;
    std::cout << std::endl << std::endl;

    return 0;
}

For installation details :

sudo apt-get install libcrypto++-dev libcrypto++-doc libcrypto++-utils

Sean Wei
  • 7,433
  • 1
  • 19
  • 39
berkay
  • 3,907
  • 4
  • 36
  • 51
  • 2
    i added cryptlib.lib in the additional dependencies and tried to build it on VS2010 but got 47 linking errors most are like the following :- error LNK2005: _tolower already defined in MSVCRTD.lib(MSVCR100D.dll) , any help in that? – yohannist Sep 06 '12 at 20:06
  • @user1470033 , i just added some installation links. – berkay Sep 06 '12 at 20:20
  • I accidentally clicked the Flag icon in your last comment. I hope it didn't harm your points! Sorry. –  Mar 11 '13 at 05:13
  • I ran this code, but I'm getting a mysterious character at the end of the decrypted string. I outputted the text as hex bytes, and saw that the last character is a `0x0`. Any idea what could be causing this? – Alexander Dec 28 '13 at 22:43
  • The first AES example I've seen that just works... Bravo! – C.J. Feb 17 '14 at 23:06
  • 1
    I don't get where key is initialized,how should I initialize it with a std::string type with unknown size? – Shohreh Jun 09 '14 at 11:46
  • It really works with mine setting with CryptoPP::AES::MAX_KEYLENGTH. Thanks! – haxpor Jun 17 '14 at 12:29
  • 1
    @AMomchilov, the reason is that when the input byte array are using the code `plaintext.length() + 1`, thus the last `0x0` is add to the input byte array of the encoder. I just see the size of plaintext is 55, but the size of decryptedtext is 56. I think we should use `plaintext.length()`, not `plaintext.length() +1`. – ollydbg23 Dec 09 '15 at 07:21
  • @berkay, do you know why the algorithm crashes if I try to decrypt a message using a wrong key? Thanks, Massimo – user1738687 May 24 '17 at 14:18
  • @user1738687, It has been for a while not touching this topic. I suggest catching the exception, and print the key error if it solves your problem. – berkay Dec 28 '17 at 21:08
  • @c00000fd "AES-128 is not doing well and may be broken in the next 5-10 years." if AES-128 is broken, AES-256 is also broken, since there is a well known attack, that reduces the complexity to 98 Bits of the AES-256 chiffre. – Shalec Apr 11 '18 at 05:52
  • 1
    @Shalec: Yes, it was a silly statement. I removed it. – c00000fd Apr 11 '18 at 08:25
  • thanks for the code sample, just had to change `byte` to `CryptoPP::byte` – Micka Jun 13 '18 at 10:02
  • 1
    This mostly worked for me, but I found that `decryptedtext` string was one byte longer than the `plaintext` as it has an additional trailing null byte. I also had to fix the missing namespace for the `byte` type, as pointed out by @Micka. I updated the answer with these fixes. – Arthur Tacca Feb 07 '19 at 11:45
  • 1
    @Shohreh, Late to the party, but he initializes the key in memset command. – Roy2511 Apr 15 '20 at 10:15