Crypto++学习总结---AES

发布时间:2017-1-18 18:02:36 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Crypto++学习总结---AES",主要涉及到Crypto++学习总结---AES方面的内容,对于Crypto++学习总结---AES感兴趣的同学可以参考一下。

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))); }

上一篇:[置顶] DBA日常管理——数据归档(Archiving-Data)
下一篇:platform _device和platform_driver注册过程

相关文章

相关评论