Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $YECBGYFECGEAFWHA as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2

Deprecated: imwpcache\f884414bce24ee67f\f73723ec7b1919fa5::__construct(): Implicitly marking parameter $BBWFDDBHHYHDXXAB as nullable is deprecated, the explicit nullable type must be used instead in /www/wwwroot/www.chuangxiangniao.com/wp-content/plugins/imwpcache-dist/build/f884414bce24ee67ff73723ec7b1919fa5.php on line 2
c++怎么使用OpenSSL进行加密和解密_c++ OpenSSL加解密方法_创想鸟

c++怎么使用OpenSSL进行加密和解密_c++ OpenSSL加解密方法

答案: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进行加密和解密_c++ openssl加解密方法

在C++中使用OpenSSL进行加密和解密,通常涉及对称加密(如AES)和非对称加密(如RSA)。下面以AES和RSA为例,介绍基本的加解密方法。确保已安装OpenSSL开发库,并在编译时链接-lssl -lcrypto。

AES对称加密(CBC模式)

AES是一种常用的对称加密算法,适合加密大量数据。以下是一个使用AES-256-CBC的简单示例:

加密函数示例:

#include #include #include #include 

std::vector aes_encrypt(const std::string& plaintext, const unsigned char* key) {AES_KEY enc_key;AES_set_encrypt_key(key, 256, &enc_key);

std::vector ciphertext(plaintext.size() + AES_BLOCK_SIZE);std::vector iv(AES_BLOCK_SIZE);RAND_bytes(iv.data(), AES_BLOCK_SIZE); // 生成随机IVint out_len = 0;AES_cbc_encrypt(    reinterpret_cast(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& ciphertext, const unsigned char* key) {    AES_KEY dec_key;    AES_set_decrypt_key(key, 256, &dec_key);
std::vector iv(ciphertext.begin(), ciphertext.begin() + AES_BLOCK_SIZE);std::vector 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非对称加密

RSA常用于加密密钥或小量数据。需要先生成密钥对(可用OpenSSL命令行工具生成PEM文件)。

从PEM文件加载RSA公钥并加密:

#include #include #include #include 

std::vector rsa_encrypt(const std::string& plaintext, const std::string& pubkey_path) {FILE fp = fopen(pubkey_path.c_str(), "r");RSA rsa = PEM_read_RSA_PUBKEY(fp, NULL, NULL, NULL);fclose(fp);

int rsa_len = RSA_size(rsa);std::vector ciphertext(rsa_len);int result = RSA_public_encrypt(    plaintext.size(),    reinterpret_cast(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& 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);
int rsa_len = RSA_size(rsa);std::vector 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时需注意以下几点:

确保正确处理错误,可通过ERR_print_errors_fp(stderr)查看错误信息密钥和IV必须安全生成,避免硬编码对大数据加密时,建议使用AES加密数据,再用RSA加密AES密钥(混合加密)释放OpenSSL对象(如RSA、EVP_CIPHER_CTX)避免内存泄漏新版OpenSSL推荐使用EVP接口,更安全且支持多种算法统一调用

基本上就这些。实际项目中建议封装成类,并加入异常处理和日志。

以上就是c++++怎么使用OpenSSL进行加密和解密_c++ OpenSSL加解密方法的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1477949.html

赞 (0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
c++中如何初始化类的静态成员_类静态成员变量初始化方法
上一篇 2025年12月19日 02:29:33
c++中string如何转换为int_c++ string转int类型实现方法
下一篇 2025年12月19日 02:29:50

相关推荐

发表回复

登录后才能评论
关注微信