Click here to Skip to main content
15,893,923 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.3K   10.9K   157  
Encrypt data using Block Ciphers with Crypto++.
// CTS.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

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

#include <iostream>

using namespace CryptoPP;
using namespace std;

int main(int argc, char* argv[])
{
  byte key[ ThreeWay::DEFAULT_KEYLENGTH ];
  byte iv[ ThreeWay::BLOCKSIZE ];

  memset( key, 0x00, ThreeWay::DEFAULT_KEYLENGTH );
  memset( iv, 0x00, ThreeWay::BLOCKSIZE );  

  // Problem at srtlen( plain )  = 12
  // string plain = "Hello World!";
  string plain = "Hello World!!";

  string cipher, recovered;



  ////////////////////
  // Encryption
  ////////////////////
  ThreeWayEncryption twe( key, ThreeWay::DEFAULT_KEYLENGTH );
  CBC_CTS_Mode_ExternalCipher::Encryption encryptor( twe, iv );

  StreamTransformationFilter stfe( encryptor, new StringSink( cipher ));
  stfe.Put( (byte*)plain.c_str(), plain.size() );
  stfe.MessageEnd();

  ////////////////////
  // Decryption
  ////////////////////
  ThreeWayDecryption twd( key, ThreeWay::DEFAULT_KEYLENGTH );
  CBC_CTS_Mode_ExternalCipher::Decryption decryptor( twd, iv );

  StreamTransformationFilter stfd( decryptor, new StringSink( recovered ));
  stfd.Put((byte*)cipher.c_str(), cipher.size() );
  stfd.MessageEnd();

  std::cout << "Recovered Text: " << recovered << std::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