答案:C++中使用OpenSSL进行AES和RSA加解密需先安装OpenSSL库并链接-lssl -lcrypto;AES-256-CBC用于加密大量数据,通过AES_set_encrypt_key设置密钥,RAND_bytes生成随机IV,AES_cbc_encrypt执行加密,并将IV置于密文前;解密时提取IV,调用AES_cbc_encrypt解密后去除PKCS#7填充;RSA用于加密小数据或密钥,通过PEM_read_RSA_PUBKEY加载公钥,RSA_public_encrypt加密,PEM_read_RSAPrivateKey加载私钥,RSA_private_decrypt解密;需注意错误处理、密钥安全、内存释放,推荐使用EVP接口,实际项目应封装成类并添加异常处理。

在C++中使用OpenSSL进行加密和解密,通常涉及对称加密(如AES)和非对称加密(如RSA)。下面以AES和RSA为例,介绍基本的加解密方法。确保已安装OpenSSL开发库,并在编译时链接-lssl -lcrypto。
AES是一种常用的对称加密算法,适合加密大量数据。以下是一个使用AES-256-CBC的简单示例:
加密函数示例:
#include <openssl/aes.h>
#include <openssl/rand.h>
#include <vector>
#include <iostream>
<p>std::vector<unsigned char> aes_encrypt(const std::string& plaintext, const unsigned char* key) {
AES_KEY enc_key;
AES_set_encrypt_key(key, 256, &enc_key);</p><pre class='brush:php;toolbar:false;'>std::vector<unsigned char> ciphertext(plaintext.size() + AES_BLOCK_SIZE);
std::vector<unsigned char> iv(AES_BLOCK_SIZE);
RAND_bytes(iv.data(), AES_BLOCK_SIZE); // 生成随机IV
int out_len = 0;
AES_cbc_encrypt(
reinterpret_cast<const unsigned char*>(plaintext.c_str()),
ciphertext.data() + AES_BLOCK_SIZE,
plaintext.size(),
&enc_key,
iv.data(),
AES_ENCRYPT
);
// 将IV放在密文前面
ciphertext.insert(ciphertext.begin(), iv.begin(), iv.end());
return ciphertext;}
立即学习“C++免费学习笔记(深入)”;
解密函数示例:
std::string aes_decrypt(const std::vector<unsigned char>& ciphertext, const unsigned char* key) {
AES_KEY dec_key;
AES_set_decrypt_key(key, 256, &dec_key);
<pre class='brush:php;toolbar:false;'>std::vector<unsigned char> iv(ciphertext.begin(), ciphertext.begin() + AES_BLOCK_SIZE);
std::vector<unsigned char> decrypted(ciphertext.size() - AES_BLOCK_SIZE);
AES_cbc_encrypt(
ciphertext.data() + AES_BLOCK_SIZE,
decrypted.data(),
decrypted.size(),
&dec_key,
iv.data(),
AES_DECRYPT
);
// 去除PKCS#7填充
int pad_len = decrypted.back();
decrypted.resize(decrypted.size() - pad_len);
return std::string(decrypted.begin(), decrypted.end());}
立即学习“C++免费学习笔记(深入)”;
RSA常用于加密密钥或小量数据。需要先生成密钥对(可用OpenSSL命令行工具生成PEM文件)。
从PEM文件加载RSA公钥并加密:
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <fstream>
#include <vector>
<p>std::vector<unsigned char> rsa_encrypt(const std::string& plaintext, const std::string& pubkey_path) {
FILE<em> fp = fopen(pubkey_path.c_str(), "r");
RSA</em> rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);
fclose(fp);</p><pre class='brush:php;toolbar:false;'>int rsa_len = RSA_size(rsa);
std::vector<unsigned char> ciphertext(rsa_len);
int result = RSA_public_encrypt(
plaintext.size(),
reinterpret_cast<const unsigned char*>(plaintext.c_str()),
ciphertext.data(),
rsa,
RSA_PKCS1_PADDING
);
RSA_free(rsa);
if (result == -1) {
return {};
}
ciphertext.resize(result);
return ciphertext;}
立即学习“C++免费学习笔记(深入)”;
用私钥解密:
std::string rsa_decrypt(const std::vector<unsigned char>& ciphertext, const std::string& privkey_path) {
FILE* fp = fopen(privkey_path.c_str(), "r");
RSA* rsa = PEM_read_RSAPrivateKey(fp, NULL, NULL, NULL);
fclose(fp);
<pre class='brush:php;toolbar:false;'>int rsa_len = RSA_size(rsa);
std::vector<unsigned char> decrypted(rsa_len);
int result = RSA_private_decrypt(
ciphertext.size(),
ciphertext.data(),
decrypted.data(),
rsa,
RSA_PKCS1_PADDING
);
RSA_free(rsa);
if (result == -1) {
return "";
}
decrypted.resize(result);
return std::string(decrypted.begin(), decrypted.end());}
立即学习“C++免费学习笔记(深入)”;
使用OpenSSL时需注意以下几点:
基本上就这些。实际项目中建议封装成类,并加入异常处理和日志。
以上就是c++++怎么使用OpenSSL进行加密和解密_c++ OpenSSL加解密方法的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号