欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 艺术 > 使用C++的OpenSSL 库实现 AES 加密和解密文件

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

2024/10/24 20:15:18 来源:https://blog.csdn.net/2301_80892630/article/details/142709065  浏览:    关键词:使用C++的OpenSSL 库实现 AES 加密和解密文件

        如果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++ 等加密库,还能深入理解现代加密技术的工作原理,希望这个设计思路和实现步骤能对你有帮助。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com