Click here to Skip to main content
15,896,154 members
Articles / Desktop Programming / Win32

Applied Crypto++: Block Ciphers

Rate me:
Please Sign up or sign in to vote.
4.85/5 (50 votes)
6 Apr 2008CPOL30 min read 245.4K   10.9K   157  
Encrypt data using Block Ciphers with Crypto++.
// Sample.cpp
//

#include "stdafx.h"

// Runtime Includes
#include <iostream>
#include <string>

// Crypto++ Includes
#include "aes.h"
#include "modes.h" // xxx_Mode< >
#include "filters.h" // StringSource and
                     // StreamTransformation
#include "osrng.h"
#include "hex.h"

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

    ///////////////////////////////////////
    // Key and Counter //
    ///////////////////////////////////////

    byte key[ CryptoPP::AES::DEFAULT_KEYLENGTH ];
    byte counter[ CryptoPP::AES::BLOCKSIZE ];
    byte next[ CryptoPP::AES::BLOCKSIZE ];  // next counter

    ::memset( key, 0xAA, CryptoPP::AES::DEFAULT_KEYLENGTH );
    ::memset( counter, 0x00, CryptoPP::AES::BLOCKSIZE );

    ///////////////////////////////////////
    // Sources and Sinks                 //
    ///////////////////////////////////////

    // Message M
    std::string PlainText;

    // Cipher Text Sink
    std::string CipherText;

    // Recovered Text Sink
    std::string RecoveredText;

    ///////////////////////////////////////
    // Random Data                       //
    ///////////////////////////////////////
    CryptoPP::AutoSeededRandomPool rng;
    CryptoPP::RandomNumberSource( rng,
        CryptoPP::AES::BLOCKSIZE,
        true, new CryptoPP::StringSink( PlainText )
    );

    ///////////////////////////////////////
    // Encryption                        //
    ///////////////////////////////////////

    CryptoPP::CTR_Mode< CryptoPP::AES >::Encryption Encryptor;
    Encryptor.SetKeyWithIV( key, sizeof(key), counter );

    CryptoPP::StringSource( PlainText, true,
        new CryptoPP::StreamTransformationFilter(
          Encryptor,
          new CryptoPP::StringSink( CipherText )
        ) // StreamTransformationFilter
    ); // StringSource

    ///////////////////////////////////////
    // Next Counter Value                //
    ///////////////////////////////////////
    Encryptor.GetNextIV( rng, next );

    ///////////////////////////////////////
    // Decryption                        //
    ///////////////////////////////////////

    CryptoPP::CTR_Mode< CryptoPP::AES >::Decryption Decryptor;
    Decryptor.SetKeyWithIV( key, sizeof(key), counter );

    CryptoPP::StringSource( CipherText, true,
        new CryptoPP::StreamTransformationFilter(
          Decryptor,
          new CryptoPP::StringSink( RecoveredText )
        ) // StreamTransformationFilter
    ); // StringSource

    //////////////////////////////////////////
    // Output                               //
    //////////////////////////////////////////

    cout << "Algorithm: ";
    cout << Encryptor.AlgorithmName() << endl;
    cout << endl;

    cout << "Default Key Size: ";
    cout << Encryptor.DefaultKeyLength() << " bytes" << endl;

    cout << "Block Size: ";
    cout << Encryptor.BlockSize() << " bytes" << endl;
    cout << endl;

    cout << "Plain Text: " << PlainText.length() << " bytes" << endl;
    cout << "Cipher Text: " << CipherText.length() << " bytes" << endl;
    cout << "Recovered Text:" << RecoveredText.length() << " bytes" << endl;
    cout << endl;

    //////////////////////////////////////////
    // Original Counter
    //////////////////////////////////////////
    std::string hexbytes;
    CryptoPP::ArraySource(
        (const byte*)counter, CryptoPP::AES::BLOCKSIZE, true,
        new CryptoPP::HexEncoder(
            new CryptoPP::StringSink( hexbytes ), true, 2
        )
    );

    cout << "Original Counter: " << endl;
    cout << hexbytes << endl;

    //////////////////////////////////////////
    // Next Counter
    //////////////////////////////////////////
    hexbytes.clear();
    CryptoPP::ArraySource(
        (const byte*)next, CryptoPP::AES::BLOCKSIZE, true,
        new CryptoPP::HexEncoder(
            new CryptoPP::StringSink( hexbytes ), true, 2
        )
    );

    cout << "Next Counter: " << endl;
    cout << hexbytes << endl;

    return 0;
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems / Hardware Administrator
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions