使用C++的OpenSSL 库实现 AES 加密和解密文件

ops/2024/10/22 19:22:38/

        如果C++不知道做什么项目,可以编写一个文件加密和解密工具,支持诸如 AES 和 RSA 等常见的加密算法。这样的项目可以帮助学习和理解现代加密技术,并为日常文件保护提供便利。以下是一个基本的设计思路和实现步骤:

1. 设计思路

a. 功能需求
  1. 文件加密:支持对文件进行加密。
  2. 文件解密:支持对加密文件进行解密。
  3. 加密算法选择:支持选择不同的加密算法,如 AES、RSA 等。
  4. 密钥管理:生成和管理加密密钥。
  5. 文件操作:能够读取和写入文件。
b. 技术选型
  1. 加密库:使用 OpenSSL 或 Crypto++ 等成熟的加密库来实现加密和解密功能。
  2. 文件 I/O:使用 C++ 标准库中的文件操作功能来读取和写入文件。
  3. 命令行界面:使用简单的命令行界面(CLI)来接收用户输入和显示结果。

2. 实现步骤

a. 环境准备
  • 安装 OpenSSL 或 Crypto++ 库。
  • 创建一个新的 C++ 项目。
b. 使用 OpenSSL 实现 AES 加密和解密

OpenSSL 提供了丰富的加密功能,以下是如何实现 AES 加密和解密的示例。

#include <openssl/evp.h>
#include <openssl/rand.h>
#include <iostream>
#include <fstream>
#include <vector>
#include <iostream>
#include <string>
// 以上是下面加密算法和解密算法要用到的头文件
参数:
  • const std::string& file:要加密的文件的路径。
  • const std::string& algorithm:使用的加密算法(目前仅支持 “aes”)。

生成 AES-256 加密所需的 32 字节密钥(key)和 16 字节初始化向量(IV),使用 RAND_bytes 函数生成随机字节。同时将生成的密钥和 IV 写入一个名为 file.key 的文件中,以便解密时使用。 


void encrypt_file(const std::string& file, const std::string& algorithm) {// 也就是说当前仅支持AES加密算法if (algorithm != "aes") {std::cerr << "不支持该加密算法: " << algorithm << std::endl;return;}const unsigned int key_len = 32; // AES-256 key lengthconst unsigned int iv_len = 16;  // IV lengthunsigned char key[key_len], iv[iv_len];// 生成密钥和 IVRAND_bytes(key, key_len);RAND_bytes(iv, iv_len);// 保存密钥和 IVstd::ofstream key_file(file + ".key", std::ios::binary);key_file.write((char*)key, key_len);key_file.write((char*)iv, iv_len);key_file.close();// 打开输入文件std::ifstream infile(file, std::ios::binary);if (!infile) {std::cerr << "无法打开文件: " << file << std::endl;return;}// 打开输出文件std::ofstream outfile(file + ".enc", std::ios::binary);if (!outfile) {std::cerr << "无法新建输出文件." << std::endl;return;}// 初始化加密上下文EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv);// 加密文件unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];int inlen, outlen;while (infile.good()) {infile.read((char*)inbuf, sizeof(inbuf));inlen = infile.gcount();if (inlen > 0) {EVP_EncryptUpdate(ctx, outbuf, &outlen, inbuf, inlen);outfile.write((char*)outbuf, outlen);}}// 结束加密EVP_EncryptFinal_ex(ctx, outbuf, &outlen);outfile.write((char*)outbuf, outlen);// 清理EVP_CIPHER_CTX_free(ctx);infile.close();outfile.close();std::cout << "文件加密成功: " << file + ".enc" << std::endl;
}

void decrypt_file(const std::string& file, const std::string& algorithm) {if (algorithm != "aes") {std::cerr << "不支持该算法: " << algorithm << std::endl;return;}const unsigned int key_len = 32; // AES-256 key lengthconst unsigned int iv_len = 16;  // IV lengthunsigned char key[key_len], iv[iv_len];// 读取密钥和 IVstd::ifstream key_file(file + ".key", std::ios::binary);if (!key_file) {std::cerr << "无法打开密钥文件." << std::endl;return;}key_file.read((char*)key, key_len);key_file.read((char*)iv, iv_len);key_file.close();// 打开输入文件std::ifstream infile(file, std::ios::binary);if (!infile) {std::cerr << "无法打开输入文件: " << file << std::endl;return;}// 打开输出文件std::ofstream outfile(file + ".dec", std::ios::binary);if (!outfile) {std::cerr << "无法创建输出文件." << std::endl;return;}// 初始化解密上下文EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv);// 解密文件unsigned char inbuf[1024], outbuf[1024 + EVP_MAX_BLOCK_LENGTH];int inlen, outlen;while (infile.good()) {infile.read((char*)inbuf, sizeof(inbuf));inlen = infile.gcount();if (inlen > 0) {EVP_DecryptUpdate(ctx, outbuf, &outlen, inbuf, inlen);outfile.write((char*)outbuf, outlen);}}// 结束解密EVP_DecryptFinal_ex(ctx, outbuf, &outlen);outfile.write((char*)outbuf, outlen);// 清理EVP_CIPHER_CTX_free(ctx);infile.close();outfile.close();std::cout << "文件解密成功: " << file + ".dec" << std::endl;
}
 c. 命令行界面设计

设计一个简单的命令行界面,接收用户的输入并执行相应的操作。


void show_help() {std::cout << "用法: encryptor [options]" << std::endl;std::cout << "参数选项:" << std::endl;std::cout << "  -e <file> <algorithm>  加密文件" << std::endl;std::cout << "  -d <file> <algorithm>  解密文件" << std::endl;std::cout << "  -h                     帮助信息" << std::endl;
}int main(int argc, char* argv[]) {if (argc < 2) {show_help();return 1;}std::string option = argv[1];if (option == "-h") {show_help();} else if (option == "-e" && argc == 4) {std::string file = argv[2];std::string algorithm = argv[3];// 调用加密函数encrypt_file(file, algorithm);} else if (option == "-d" && argc == 4) {std::string file = argv[2];std::string algorithm = argv[3];// 调用解密函数decrypt_file(file, algorithm);} else {std::cerr << "无效参数" << std::endl;show_help();return 1;}return 0;
}

3. 运行和测试

编译并运行你的程序,确保能够正确加密和解密文件。可以通过以下命令进行测试:encryptor是你生成的可执行文件名称

# 加密文件
./encryptor -e input.txt aes# 解密文件
./encryptor -d input.txt.enc aes

4. 扩展功能

你可以进一步扩展这个工具的功能,例如:

  • 支持多种加密算法:如 RSA、DES 等。
  • 用户输入密钥:允许用户输入或选择密钥。
  • 图形用户界面:使用 Qt 或 wxWidgets 等库来创建一个图形界面。

总结

通过这个项目,你不仅可以学习到如何使用 OpenSSL 或 Crypto++ 等加密库,还能深入理解现代加密技术的工作原理,希望这个设计思路和实现步骤能对你有帮助。


http://www.ppmy.cn/ops/124141.html

相关文章

CSRF | POST 型 CSRF 漏洞攻击

关注这个漏洞的其他相关笔记&#xff1a;CSRF 漏洞 - 学习手册-CSDN博客 0x01&#xff1a;POST 型 CSRF 漏洞攻击 —— 理论篇 POST 型 CSRF 漏洞是指攻击者通过构造恶意的 HTTP POST 请求&#xff0c;利用用户的登录状态&#xff0c;在用户不知情的情况下&#xff0c;诱使浏览…

声波定位给我们日常生活带来哪些便利

在科技日新月异的今天&#xff0c;声波定位技术作为一项前沿科技&#xff0c;正悄然改变着我们的生活方式&#xff0c;为日常生活带来了前所未有的便利与惊喜。这项技术&#xff0c;通过发射声波并接收其反射回来的信号&#xff0c;精确测量物体位置或距离&#xff0c;不仅在科…

Kubernetes(K8s)的简介

一、Kubernetes的简介 1 应用部署方式演变 在部署应用程序的方式上&#xff0c;主要经历了三个阶段&#xff1a; 传统部署&#xff1a;互联网早期&#xff0c;会直接将应用程序部署在物理机上 优点&#xff1a;简单&#xff0c;不需要其它技术的参与 缺点&#xff1a;不能为应…

GR-ConvNet论文 学习笔记

GR-ConvNet 文章目录 GR-ConvNet前言一、引言二、相关研究三、问题阐述四、方法A.推理模块B.控制模块C.模型结构D.训练方法E.损失函数 五、评估A.数据集B.抓取评判标准 六、实验A.设置B.家庭测试物体C.对抗性测试物体D.混合物体 七、结果A.康奈尔数据集B.Jacquard数据集C.抓取新…

git clone 私有仓库时出现错误 Authentication failed for :xxxxxx

错误信息 remote: Support for password authentication was removed on August 13, 2021. remote: Please see https://docs.github.com/get-started/getting-started-with-git/about-remote-repositories#cloning-with-https-urls for information on currently recommended…

低代码赋能汽车制造产业链场景系列

当前汽车行业数字化智能化转型浪潮下&#xff0c;整车及其上下游产业链的协同创新正变得至关重要。头部车企与上下游供应链企业正逐步解决在生产管理、业务互通、系统集成等方面的痛点与挑战。电动化、智能化、网联化作为汽车产业的三大趋势&#xff0c;正共同推动未来汽车产业…

软件验证与确认实验二-单元测试

目录 1. 实验目的及要求.................................................................................................... 3 2. 实验软硬件环境.................................................................................................... 3 …

2024年网络安全进阶手册:黑客技术自学路线

&#x1f91f; 基于入门网络安全/黑客打造的&#xff1a;&#x1f449;黑客&网络安全入门&进阶学习资源包 前言 什么是网络安全 网络安全可以基于攻击和防御视角来分类&#xff0c;我们经常听到的 “红队”、“渗透测试” 等就是研究攻击技术&#xff0c;而“蓝队”、…