您的位置:首页 > 其它

Crypto++学习总结---AES

2015-11-20 16:01 441 查看
静态库下载连接Cryp++ lib 下载

AES 使用方法 如下:

[cpp] view
plaincopy





//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)

);



}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: