切换主题
C++ 加密库
本文档介绍常用的 C++ 加密库及其使用方法,包括 OpenSSL 和 Crypto++。
OpenSSL
简介
OpenSSL 是一个开源的加密库,支持多种加密算法(如 AES、RSA、SHA),广泛应用于网络通信和数据加密。
安装与配置
- 安装 OpenSSL:
- 在 Linux 上:bash
sudo apt-get install libssl-dev
1 - 在 Windows 上,通过 vcpkg 安装:bash
vcpkg install openssl
1
- 在 Linux 上:
- 在项目中包含头文件:cpp
#include <openssl/evp.h> #include <openssl/rand.h>
1
2
示例代码
以下是一个使用 OpenSSL 实现 AES-256-CBC 加密的示例:
cpp
// filepath: d:\Docs\huaqiwill-dev-docs\编程语言\C++\扩展库\C++加密库.md
#include <openssl/evp.h>
#include <openssl/rand.h>
#include <iostream>
#include <vector>
#include <cstring>
void handleErrors() {
std::cerr << "Error occurred!" << std::endl;
exit(1);
}
std::vector<unsigned char> aes_encrypt(const std::string& plaintext, const std::vector<unsigned char>& key, const std::vector<unsigned char>& iv) {
EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
if (!ctx) handleErrors();
if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key.data(), iv.data()) != 1) handleErrors();
std::vector<unsigned char> ciphertext(plaintext.size() + EVP_CIPHER_block_size(EVP_aes_256_cbc()));
int len = 0, ciphertext_len = 0;
if (EVP_EncryptUpdate(ctx, ciphertext.data(), &len, reinterpret_cast<const unsigned char*>(plaintext.data()), plaintext.size()) != 1) handleErrors();
ciphertext_len += len;
if (EVP_EncryptFinal_ex(ctx, ciphertext.data() + len, &len) != 1) handleErrors();
ciphertext_len += len;
ciphertext.resize(ciphertext_len);
EVP_CIPHER_CTX_free(ctx);
return ciphertext;
}
int main() {
std::string plaintext = "Hello, OpenSSL!";
std::vector<unsigned char> key(32), iv(16);
if (!RAND_bytes(key.data(), key.size()) || !RAND_bytes(iv.data(), iv.size())) handleErrors();
auto ciphertext = aes_encrypt(plaintext, key, iv);
std::cout << "Ciphertext size: " << ciphertext.size() << std::endl;
return 0;
}
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
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
Crypto++
简介
Crypto++ 是一个 C++ 加密库,支持多种加密算法(如 AES、RSA、SHA),并提供简单易用的接口。
安装与配置
- 下载 Crypto++ 源码:bash
git clone https://github.com/weidai11/cryptopp.git
1 - 编译并安装:bash
make sudo make install
1
2 - 在项目中包含头文件:cpp
#include <cryptlib.h> #include <aes.h> #include <modes.h> #include <osrng.h>
1
2
3
4
示例代码
以下是一个使用 Crypto++ 实现 AES-256-CBC 加密的示例:
cpp
#include <cryptlib.h>
#include <aes.h>
#include <modes.h>
#include <osrng.h>
#include <iostream>
#include <string>
int main() {
using namespace CryptoPP;
std::string plaintext = "Hello, Crypto++!";
SecByteBlock key(AES::DEFAULT_KEYLENGTH);
SecByteBlock iv(AES::BLOCKSIZE);
AutoSeededRandomPool prng;
prng.GenerateBlock(key, key.size());
prng.GenerateBlock(iv, iv.size());
std::string ciphertext;
try {
CBC_Mode<AES>::Encryption encryptor(key, key.size(), iv);
StringSource(plaintext, true, new StreamTransformationFilter(encryptor, new StringSink(ciphertext)));
} catch (const Exception& e) {
std::cerr << "Encryption error: " << e.what() << std::endl;
return 1;
}
std::cout << "Ciphertext size: " << ciphertext.size() << std::endl;
return 0;
}
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
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
对比
特性 | OpenSSL | Crypto++ |
---|---|---|
安装方式 | 系统包管理器或源码 | 源码编译 |
学习曲线 | 较陡峭 | 较平缓 |
适用场景 | 网络通信、SSL/TLS | 嵌入式、快速开发 |
性能 | 高性能 | 性能优秀 |