Crypto++学习总结---AES

静态库下载连接Cryp++ lib 下载

AES 使用方法 如下:

//For AES encrypt
#include "default.h" 
#include "cryptlib.h"
#include "filters.h"
#include "bench.h"
#include "osrng.h"
#include "hex.h"
#include "modes.h"
#include "files.h"

using namespace CryptoPP;
#pragma comment(lib, "cryptopp\\lib\\cryptlib.lib") 

using namespace std;

void main() {

	unsigned char key[]	= {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,	0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x08};//AES::DEFAULT_KEYLENGTH
	unsigned char iv[]	= {0x01,0x02,0x03,0x03,0x03,0x03,0x03,0x03,	0x03,0x03, 0x01,0x02,0x03,0x03,0x03,0x03};
	int keysize = 16;

	string	message = "Hello World!";
	string	strEncTxt;
	string	strDecTxt;

	//CBC - PADDING
	//AES-CBC Encrypt(ONE_AND_ZEROS_PADDING)
	CBC_Mode<AES>::Encryption  Encryptor1(key,keysize,iv); 
	StringSource(	message,
		true,
		new StreamTransformationFilter(	Encryptor1,
		new StringSink( strEncTxt ),
		BlockPaddingSchemeDef::BlockPaddingScheme::ONE_AND_ZEROS_PADDING,
		true)
		);

	//AES-CBC Decrypt
	CBC_Mode<AES>::Decryption Decryptor1(key,keysize,iv); 
	StringSource(	strEncTxt, 
		true,
		new StreamTransformationFilter( Decryptor1,
		new StringSink( strDecTxt ),
		BlockPaddingSchemeDef::BlockPaddingScheme::ONE_AND_ZEROS_PADDING,
		true)
		);


	//CTR - NO_PADDING
	//AES-CTR Encrypt
	CTR_Mode<AES>::Encryption  Encryptor2(key,keysize,iv); 
	StringSource(	message, 
		true,
		new StreamTransformationFilter( Encryptor2,
		new StringSink( strEncTxt ),
		BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING,
		true)
		); 

	//AES-CTR Decrypt
	CTR_Mode<AES>::Decryption Decryptor2(key,keysize,iv); 
	StringSource(	strEncTxt, 
		true,
		new StreamTransformationFilter( Decryptor2,
		new StringSink( strDecTxt ),
		BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING,
		true)
		);  

}

     这里也可以进行文件的加密:

SecByteBlock HexDecodeString(const char *hex) {
	StringSource ss(hex, true, new HexDecoder);
	SecByteBlock result((size_t)ss.MaxRetrievable());
	ss.Get(result, result.size());
	return result;
}

void AES_CTR_Encrypt(const char *hexKey, const char *hexIV, const char *infile, const char *outfile) {
	SecByteBlock key = HexDecodeString(hexKey);
	SecByteBlock iv = HexDecodeString(hexIV);

	CTR_Mode<AES>::Encryption aes(key, key.size(), iv);

	FileSource(infile, true, new StreamTransformationFilter(aes, new FileSink(outfile)));
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值