免费C++库,用于密码方案,最初由Wei Dai编写,包括密码,消息认证代码,单向哈希函数,公钥密码系统,密钥协议方案和压缩压缩。
PEM包:消息加密的部分实现,允许您读取和写入 PEM 编码的密钥和参数,包括加密的私钥。其他文件包括对 RSA、DSA、EC、ECDSA 密钥和 Diffie-Hellman 参数的支持。该包包括五个额外的源文件,一个使用OpenSSL创建测试密钥的脚本,一个用于测试读取和写入密钥的C++程序,以及一个用于验证使用OpenSSL编写的Crypto++编写的密钥的脚本。
https://cryptopp.com/wiki/Main_Page
右击“Header Files”->添加->现有项:
- pem.h
- pem_common.h
右击“Source Files”->添加->现有项:
- pem_common.cpp
- pem_read.cpp
- pem_write.cpp
5. build子工程cryptlib,生成lib文件
分别在Release和debug两个情况下,调整设备型号(本机是x64),右击子工程cryptlib点击“生成”。输出显示成功后即为完成。
此时,crypto++源文件夹下会多出一个名称为x64的文件夹,该文件夹下有三个子文件夹,其中一个名为Output,Output文件夹下有两个子文件夹,如图所示。Debug和Release文件夹下都有lib文件,这两个lib文件就是我们后面要使用的lib库。
#include "cryptlib.h"
#include "rijndael.h"
#include "modes.h"
#include "files.h"
#include "osrng.h"
#include "hex.h"
#include
#include
int main(int argc, char* argv[])
{
using namespace CryptoPP;
AutoSeededRandomPool prng;
HexEncoder encoder(new FileSink(std::cout));
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
SecByteBlock iv(AES::BLOCKSIZE);
prng.GenerateBlock(key, key.size());
prng.GenerateBlock(iv, iv.size());
std::string plain = "CBC Mode Test:Hello!";
std::string cipher, recovered;
std::cout << "plain text: " << plain << std::endl;
/*********************************\
\*********************************/
try
{
CBC_Mode< AES >::Encryption e;
e.SetKeyWithIV(key, key.size(), iv);
StringSource s(plain, true,
new StreamTransformationFilter(e,
new StringSink(cipher)
) // StreamTransformationFilter
); // StringSource
}
catch (const Exception& e)
{
std::cerr << e.what() << std::endl;
exit(1);
}
/*********************************\
\*********************************/
std::cout << "key: ";
encoder.Put(key, key.size());
encoder.MessageEnd();
std::cout << std::endl;
std::cout << "iv: ";
encoder.Put(iv, iv.size());
encoder.MessageEnd();
std::cout << std::endl;
std::cout << "cipher text: ";
encoder.Put((const byte*)&cipher[0], cipher.size());
encoder.MessageEnd();
std::cout << std::endl;
/*********************************\
\*********************************/
try
{
CBC_Mode< AES >::Decryption d;
d.SetKeyWithIV(key, key.size(), iv);
StringSource s(cipher, true,
new StreamTransformationFilter(d,
new StringSink(recovered)
) // StreamTransformationFilter
); // StringSource
std::cout << "recovered text: " << recovered << std::endl;
}
catch (const Exception& e)
{
std::cerr << e.what() << std::endl;
exit(1);
}
return 0;
}
项目->属性
跟着图片进行修改选项:
a. 此处修改为我们新建的文件夹下的include文件路径。
b.留心你现在的模式是Release还是Debug。Release对应的运行库是/MT
,Debug对应的是/MTd
。
c.此处修改为我们新建的文件夹下的lib文件下路径。查看自己的配置,Debug对应的是Debug文件下的路径;Release对应的是Release文件下的路径。
d. 选择“链接器”中的“输入”,在“附加依赖项”添加“cryptlib.lib”
最后,由于crypto++的官方文档wiki资料不全面,非常感谢以下博文的贡献: