鼠标放到下面网址,点击下载即可
github地址(8.7.0版本):https://github.com/weidai11/cryptopp/releases/tag/CRYPTOPP_8_7_0
pem包官方地址:PEM Pack - Crypto++ Wiki
1、解压crypto++包和pem包,将pem文件夹中包含的全部文件复制粘贴到crypto++文件夹中
2、双击打开crypto++文件夹中的cryptest.sln工程
3、在cryptlib子工程里面添加头文件
4、在cryptlib子工程源文件中添加
5、build子工程cryptlib,生成lib文件,根据需要生成debug和release版,lib文件在x64->output里面的debug和release文件夹中
新建文件夹include,将crypto++源文件中所有头文件(.h结尾的文件)复制到include文件夹内,供后面vs工程调用。
1、打开VS(我的是2019版本),文件->新建->项目->空项目
把下面的代码复制进去
#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;
}
2、修改项目属性
(1)C/C++——常规——附加包含目录:选择我们刚刚新建的include文件夹路径
(2)链接器——常规——附加库目录:选择cryptlib.lib所在的路径
(3)连接器——输入——附加依赖项:添加cryptlib.lib
3、点击编译运行,出现以下结果则成功
#include
#include "randpool.h"
#include "rsa.h"
#include "hex.h"
#include "files.h"
#include "config.h"
#include "stdcpp.h"
#include "modes.h"
#include"base64.h"
using namespace std;
using namespace CryptoPP;
#pragma comment(lib, "cryptlib.lib")
// 加密
string encrypt(const std::string& str_in, const std::string& key, const std::string& iv)
{
std::string str_out;
CryptoPP::CFB_Mode::Encryption encryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
CryptoPP::StringSource encryptor(str_in, true,
new CryptoPP::StreamTransformationFilter(encryption,
new CryptoPP::Base64Encoder(
new CryptoPP::StringSink(str_out),
false // do not append a newline
)
)
);
return str_out;
}
//解密
string decrypt(const std::string& str_in, const std::string& key, const std::string& iv)
{
std::string str_out;
CryptoPP::CFB_Mode::Decryption decryption((byte*)key.c_str(), key.length(), (byte*)iv.c_str());
CryptoPP::StringSource decryptor(str_in, true,
new CryptoPP::Base64Decoder(
new CryptoPP::StreamTransformationFilter(decryption,
new CryptoPP::StringSink(str_out)
)
)
);
return str_out;
}
// Main函数
void main()
{
std::string str = "123456789012345"; //需要加密的字符串
std::string key = "01234567891234560123456789123456"; // 32 bytes 密钥
std::string iv = "0123456789123456"; // 16 bytes
std::string str_encrypted = encrypt(str, key, iv); //加密后密文
std::string str_decrypted = decrypt(str_encrypted, key, iv); //解密后明文
std::cout << "str_encrypted: " << str_encrypted << std::endl;
std::cout << "str_decrypted: " << str_decrypted << std::endl;
}