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

image-20240124120927967

将pem项目中的全部内容复制到cryptopp中

Cryptopp项目库生成

image-20240124121950277

打开后有四个子工程

cryptdll - 生成cryptopp.dll动态库

dlltest - 用来测试cryptopp.dll,依赖cryptdll工程

cryptlib - 生成cryptlib.lib静态库

cryptest - 用来测试cryptopp,依赖cryptlib工程

image-20240124122042469

image-20240124122244838

在crtptlib中Header Files => 添加 => 现有项

1
2
pem.h
pem_common.h

Source Files => 添加 => 现有项

1
2
3
pem_common.cpp
pem_read.cpp
pem_write.cpp

重新在DEBUG和RELEASE生成

最终会在项目目录生成x64/Output文件夹

image-20240124122916335

文件夹内容为lib库

image-20240124122929849

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

image-20240124123122265

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

设置属性

C/C++ => 常规 => 附加包含目录

image-20240124123956329

C/C++ => 代码生成 => 运行库

Release选择MT

image-20240124124046673

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

image-20240124124235343

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

image-20240124124355297

使用测试

在设置好的Release版本成功运行

image-20240124124531689

__END__