VS2022中使用Cryptopp加密库
下载地址
cryptopp:
https://www.cryptopp.com/#download
https://github.com/weidai11/cryptopp
cryptopp-pem:
https://github.com/noloader/cryptopp-pem
官方文档
https://cryptopp.com/wiki/Main_Page
下载cryptopp和cryptopp-pem

将pem项目中的全部内容复制到cryptopp中
Cryptopp项目库生成

打开后有四个子工程
cryptdll - 生成cryptopp.dll动态库
dlltest - 用来测试cryptopp.dll,依赖cryptdll工程
cryptlib - 生成cryptlib.lib静态库
cryptest - 用来测试cryptopp,依赖cryptlib工程


在crtptlib中Header Files => 添加 => 现有项
Source Files => 添加 => 现有项
1 2 3
| pem_common.cpp pem_read.cpp pem_write.cpp
|
重新在DEBUG和RELEASE生成
最终会在项目目录生成x64/Output文件夹

文件夹内容为lib库

新建文件夹crypropp和子文件夹include和lib

将cryptopp中所有.h的头文件copy到include
将Output文件夹放入lib
VS2022使用cryptopp库
新建控制台项目
替换项目为官方AES代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| #include "cryptlib.h" #include "rijndael.h" #include "modes.h" #include "files.h" #include "osrng.h" #include "hex.h"
#include <iostream> #include <string>
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) ) ); } 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) ) );
std::cout << "recovered text: " << recovered << std::endl; } catch (const Exception& e) { std::cerr << e.what() << std::endl; exit(1); }
return 0; }
|
设置属性
C/C++ => 常规 => 附加包含目录

C/C++ => 代码生成 => 运行库
Release选择MT

链接器 => 常规 => 附加库目录

链接器 => 输入 => 附加依赖项

使用测试
在设置好的Release版本成功运行

__END__