静态库下载连接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::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::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::Encryption Encryptor2(key,keysize,iv);
StringSource( message,
true,
new StreamTransformationFilter( Encryptor2,
new StringSink( strEncTxt ),
BlockPaddingSchemeDef::BlockPaddingScheme::NO_PADDING,
true)
);
//AES-CTR Decrypt
CTR_Mode::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::Encryption aes(key, key.size(), iv);
FileSource(infile, true, new StreamTransformationFilter(aes, new FileSink(outfile)));
}