Crypto++ 加密解密 (文件到内存,内存到文件,内存到内存,文件到文件)
(2014-11-05 15:20:33)
标签:
windows |
分类: C学习 |
一. 下载Crypto++
Library
Crypto++
Library http://www.vrixpzone.com/zone2/CodeRepo/vc/cryptopp552.zip
二. 建立自己使用的Crypto++
Library
从官方网下载的Crypto++开源库, 编译链接生成cryptlib.lib,然后添加Crypto++的头文件到你自己的工程中,别忘了#pragma
comment(lib, "cryptlib.lib")添加引用库 和 #include。
另外也可以添加 using
namespace CryptoPP;
下面主要介绍加密或者解密过程从内存到内存 或者是
从内存到文件 或者从文件到内存
-
-
unsigned
char key[] //AES::DEFAULT_KEYLENGTH= {0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08, 0x01,0x02, 0x03,0x04,0x05,0x06,0x07,0x08}; -
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 StreamTransformationFilt er( 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 StreamTransformationFilt er( Decryptor1, -
new StringSink( strDecTxt ), -
BlockPaddingSchemeDef::BlockPaddingScheme::ONE_AND_ZEROS_PADDING, -
true) -
); -
- //文件到内存---加密解密雷同
- const char* infilename=“testfile.txt”
-
//AES Decrypt
-
CBC_Mode::Decryption Decryptor2(key,keysize,iv); -
FileSource(infilename,
true,
-
new
StreamTransformationFilt
er( Decryptor2, -
new StringSink(
strDecTxt ),
-
BlockPaddingSchemeDef::BlockPaddingScheme::ONE_AND_ZEROS_PADDING,
- true)
- );
-
- //从内存到文件---加密解密雷同
-
const char*
outfilename=“testfile.txt” -
CBC_Mode::Encryption
Encryptor2(key,keysize,iv); -
StringSource(
strDecTxt , -
true,
-
new StreamTransformationFilt
er(Encryptor2, - new FileSink(outfilename),
-
BlockPaddingSchemeDef::BlockPaddingScheme::ONE_AND_ZEROS_PADDING,
-
true)
-
);
-
- //文件到文件
-
CBC_Mode::Encryption
Encryptor3(key,keysize,iv); -
FileSource(infile,
true, new StreamTransformationFilt newer(aes, FileSink(outfile))); -
-
参考:
http://blog.csdn.net/wangweitingaabbcc/article/details/11156721
http://www.cppblog.com/gezidan/archive/2011/08/05/152562.html
http://blog.csdn.net/wangweitingaabbcc/article/details/11170131
http://my.oschina.net/ypimgt/blog/88429?from=rss
http://blog.csdn.net/vrix/article/details/1812797
http://blog.csdn.net/lee353086/article/details/7594165