使用Crypto++进行AES加密和解密

// g++ -g3 -ggdb -O0 -DDEBUG -I/usr/include/cryptopp Driver.cpp -o Driver.exe -lcryptopp -lpthread
// g++ -g -O2 -DNDEBUG -I/usr/include/cryptopp Driver.cpp -o Driver.exe -lcryptopp -lpthread

#include "osrng.h"
#include "modes.h"
#include "base64.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include "cryptlib.h"
#include "aes.h"
#include "filters.h"

using CryptoPP::Base64Encoder;
using CryptoPP::Base64Decoder;
using CryptoPP::Exception;
using CryptoPP::StringSink;
using CryptoPP::StringSource;
using CryptoPP::StreamTransformationFilter;
using CryptoPP::AES;
using CryptoPP::CBC_Mode;
using namespace std;


// 使用AES(CBC模式)加密,返回base64编码的数据
string encrytByAES(const string &plain, const string &key, const string &iv) {
	string cipher;
	try
	{
		CBC_Mode< AES >::Encryption e;
		e.SetKeyWithIV((byte*)key.c_str(), key.size(), (byte*)iv.c_str());

		// The StreamTransformationFilter removes
		//  padding as required.
		StringSource s(plain, true, 
			new StreamTransformationFilter(e,
				new StringSink(cipher)
			) // StreamTransformationFilter
		); // StringSource
	}
	catch(const CryptoPP::Exception& e)
	{
		cerr << e.what() << endl;
	}

	// Pretty print
	string encoded;
	StringSource(cipher, true,
		new Base64Encoder(
			new StringSink(encoded)
		) // HexEncoder
	); // StringSource
	return encoded;
}

// 使用AES(CBC模式)解密,encode为base64编码的密文
string decrytByAES(const string &encode, const string &key, const string &iv) {
	string encodeByte;
	StringSource(encode, true, new Base64Decoder(
			new StringSink(encodeByte)
		));

	string recovered;
	try
	{
		CBC_Mode< AES >::Decryption d;
		d.SetKeyWithIV((byte*)key.c_str(), key.size(), (byte*)iv.c_str());

		// The StreamTransformationFilter removes
		//  padding as required.
		StringSource s(encodeByte, true, 
			new StreamTransformationFilter(d,
				new StringSink(recovered)
			) // StreamTransformationFilter
		); // StringSource
	}
	catch(const CryptoPP::Exception& e)
	{
		cerr << e.what() << endl;
	}

	return recovered;
}


int test_aes_main()
{
	cout << "----------------" << endl
		<< "Start AES test:" << endl;

	//byte key[AES::DEFAULT_KEYLENGTH];
	string key = "key 16byte";
	//byte iv[AES::BLOCKSIZE];
	string iv = "iv 16byte";
	string plain = "ABCEDDAAAVVV";

	/*********************************\
	\*********************************/
	cout << "密钥key: " << key << endl;
	cout << "初始向量iv: " << iv << endl;
	/*********************************\
	\*********************************/

	cout << "明文plain text: " << plain << endl;
	string encoded = encrytByAES(plain, key, iv);
	cout << "密文cifer text: " << encoded << endl;
	string recovered = decrytByAES(encoded, key, iv);
	cout << "解密recover text: " << recovered << endl;
	return 0;
}

 

  • 5
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值