|
Crypto++ Faq-O-Matic :
How do I use a block cipher in Crypto++ 5.x? |
| Moderator: weidai (inherited from parent)
|
First you need to decide which block cipher and which cipher mode to use: ECB, CBC, CFB, OFB, or CTR Mode. Crypto++ provides mode objects which essentially turn a block cipher into a stream cipher. Here is how to create a mode object:
#include "cryptopp/modes.h"
#include "cryptopp/aes.h"
using namespace CryptoPP;
// ...
byte key[AES::DEFAULT_KEYLENGTH], iv[AES::BLOCKSIZE];
// initialize key and iv here
CFB_Mode<AES >::Encryption cfbEncryption(key, AES::DEFAULT_KEYLENGTH, iv);
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);
Mode objects implement the SymmetricCipher interface, documented at http://www.cryptopp.com/docs/ref/struct_symmetric_cipher_documentation.html. You can use it directly, for example:
byte plaintext[100], ciphertext[100];
// put data into plaintext here
// encrypt
cfbEncryption.ProcessData(ciphertext, plaintext, 100);
// now decrypt
CFB_Mode<AES >::Decryption cfbDecryption(key, 16, iv);
cfbDecryption.ProcessData(plaintext, ciphertext, 100);
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. StreamTransformationFilter will take care of buffering data into blocks for you when needed.
std::string ciphertext;
StreamTransformationFilter cfbEncryptor(cfbEncryption, new StringSink(ciphertext));
cfbEncryptor.Put(plaintext, 100);
// input more plaintext here if needed
cfbEncryptor.MessageEnd();
return ciphertext;
2007-Aug-16 5:14pm weidai, weidai |
// To encrypt a string using a block cipher:
byte key[Twofish::DEFAULT_KEYLENGTH], iv[Twofish::BLOCKSIZE];
string plainText;
// ... populate key, iv, plainText here
string cipher;
StringSink* sink = new StringSink(cipher);
Base64Encoder* base64_enc = new Base64Encoder(sink);
CBC_Mode<Twofish>::Encryptor twofish(key, Twofish::DEFAULT_KEYLENGTH, iv);
StreamTransformationFilter* twofish_enc = new StreamTransformationFilter(twofish, base64_enc);
StringSource source(plainText, true, twofish_enc);
2003-Nov-05 6:21pm jonathanischoice |
#include <iostream>
#include <iomanip>
#include "modes.h"
#include "aes.h"
#include "filters.h"
int main(int argc, char* argv[]) {
//
// Key and IV setup
//
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() + 1 );
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<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;
}2005-Oct-21 10:38am jeffrey |
Descriptions of the modes given above can be found at the following wikipedia page:
http://en.wikipedia.org/wiki/Cipher_block_chaining 2006-Jan-21 11:08am jason |
// Runtime Includes
#include <iostream>
// Crypto++ Includes
#include "cryptlib.h"
#include "modes.h" // xxx_Mode< >
#include "filters.h" // StringSource and
// StreamTransformation
// Cipher Modes
//
// #define CIPHER_MODE CBC_CTS_Mode
// #define CIPHER_MODE CBC_Mode
// #define CIPHER_MODE CFB_FIPS_Mode
#define CIPHER_MODE CFB_Mode
// #define CIPHER_MODE CTR_Mode
// #define CIPHER_MODE ECB_Mode
// #define CIPHER_MODE OFB_Mode
// Ciphers
//
// #define CIPHER AES
// #define CIPHER Blowfish
// #define CIPHER BTEA
// #define CIPHER Camellia
// #define CIPHER CAST128
// #define CIPHER CAST256
// #define CIPHER DES
// #define CIPHER DES_EDE2
#define CIPHER DES_EDE3
// #define CIPHER DES_XEX3
// #define CIPHER GOST
// #define CIPHER IDEA
// #define CIPHER MARS
// #define CIPHER RC2
// #define CIPHER RC5
// #define CIPHER RC6
// #define CIPHER Rijndael
// #define CIPHER SAFER_K
// #define CIPHER SAFER_SK
// #define CIPHER Serpent
// #define CIPHER SHACAL2
// #define CIPHER SHARK
// #define CIPHER SKIPJACK
// #define CIPHER ThreeWay
// #define CIPHER Twofish
// #define CIPHER XTEA
int main(int argc, char* argv[]) {
// Key and IV setup
byte key[ CryptoPP::CIPHER::DEFAULT_KEYLENGTH ],
iv[ CryptoPP::CIPHER::BLOCKSIZE ];
::memset( key, 0x01, CryptoPP::CIPHER::DEFAULT_KEYLENGTH );
::memset( iv, 0x01, CryptoPP::CIPHER::BLOCKSIZE );
// Message M
std::string PlainText = "Hello World";
// Cipher Text Sink
std::string CipherText;
// Encryptor
CryptoPP::CIPHER_MODECryptoPP::CIPHER::Encryption
Encryptor( key, sizeof(key), iv );
// Encryption
CryptoPP::StringSource( PlainText, true,
new CryptoPP::StreamTransformationFilter( Encryptor,
new CryptoPP::StringSink( CipherText )
) // StreamTransformationFilter
); // StringSource
///////////////////////////////////////
// DMZ //
///////////////////////////////////////
// Recovered Text Sink
std::string RecoveredText;
// Decryptor
CryptoPP::CIPHER_MODECryptoPP::CIPHER::Decryption
Decryptor( key, sizeof(key), iv );
// Decryption
CryptoPP::StringSource( CipherText, true,
new CryptoPP::StreamTransformationFilter( Decryptor,
new CryptoPP::StringSink( RecoveredText )
) // StreamTransformationFilter
); // StringSource
//////////////////////////////////////////
// Output //
//////////////////////////////////////////
std::cout << "Algorithm:" << std::endl;
std::cout << " " << Encryptor.AlgorithmName() << std::endl;
std::cout << "Minimum Key Size:" << std::endl;
std::cout << " " << Encryptor.MinKeyLength() << " bytes" << std::endl;
std::cout << std::endl;
std::cout << "Plain Text (" << PlainText.length() << " bytes)" << std::endl;
std::cout << " '" << PlainText << "'" << std::endl;
std::cout << std::endl;
std::cout << "Recovered Text:" << std::endl;
std::cout << " '" << RecoveredText << "'" << std::endl;
std::cout << std::endl;
return 0;
}2006-Nov-23 4:22pm noloader |
| [Append to This Answer] |
| 2007-Aug-16 5:14pm
|