c++实现EVP_aes_256_cbc大文件加密与解密
时间: 2023-07-30 21:11:43 浏览: 267
c++实现文件的加密和解密
4星 · 用户满意度95%
EVP_aes_256_cbc是OpenSSL库提供的一种加密算法,可以用于对大文件进行加密和解密。下面是c++实现EVP_aes_256_cbc大文件加密与解密的示例代码:
```c++
#include <openssl/evp.h>
#include <iostream>
#include <fstream>
using namespace std;
int aes_encrypt(const char *key, const char *iv, const char *infile, const char *outfile)
{
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
cerr << "Error: EVP_CIPHER_CTX_new() failed" << endl;
return -1;
}
// 初始化加密上下文
if (EVP_EncryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char *)key, (const unsigned char *)iv) != 1) {
cerr << "Error: EVP_EncryptInit_ex() failed" << endl;
EVP_CIPHER_CTX_free(ctx);
return -1;
}
// 打开输入文件
ifstream fin(infile, ios::binary);
if (!fin.is_open()) {
cerr << "Error: Cannot open input file" << endl;
EVP_CIPHER_CTX_free(ctx);
return -1;
}
// 打开输出文件
ofstream fout(outfile, ios::binary);
if (!fout.is_open()) {
cerr << "Error: Cannot create output file" << endl;
fin.close();
EVP_CIPHER_CTX_free(ctx);
return -1;
}
// 读取输入文件并加密
const int BUFSIZE = 1024 * 1024;
unsigned char inbuf[BUFSIZE];
unsigned char outbuf[BUFSIZE + EVP_CIPHER_block_size(EVP_aes_256_cbc())];
int inlen, outlen;
while (!fin.eof()) {
fin.read((char *)inbuf, BUFSIZE);
inlen = fin.gcount();
if (EVP_EncryptUpdate(ctx, outbuf, &outlen, inbuf, inlen) != 1) {
cerr << "Error: EVP_EncryptUpdate() failed" << endl;
fin.close();
fout.close();
EVP_CIPHER_CTX_free(ctx);
return -1;
}
fout.write((char *)outbuf, outlen);
}
// 结束加密
if (EVP_EncryptFinal_ex(ctx, outbuf, &outlen) != 1) {
cerr << "Error: EVP_EncryptFinal_ex() failed" << endl;
fin.close();
fout.close();
EVP_CIPHER_CTX_free(ctx);
return -1;
}
fout.write((char *)outbuf, outlen);
// 关闭文件和加密上下文
fin.close();
fout.close();
EVP_CIPHER_CTX_free(ctx);
return 0;
}
int aes_decrypt(const char *key, const char *iv, const char *infile, const char *outfile)
{
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
cerr << "Error: EVP_CIPHER_CTX_new() failed" << endl;
return -1;
}
// 初始化解密上下文
if (EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, (const unsigned char *)key, (const unsigned char *)iv) != 1) {
cerr << "Error: EVP_DecryptInit_ex() failed" << endl;
EVP_CIPHER_CTX_free(ctx);
return -1;
}
// 打开输入文件
ifstream fin(infile, ios::binary);
if (!fin.is_open()) {
cerr << "Error: Cannot open input file" << endl;
EVP_CIPHER_CTX_free(ctx);
return -1;
}
// 打开输出文件
ofstream fout(outfile, ios::binary);
if (!fout.is_open()) {
cerr << "Error: Cannot create output file" << endl;
fin.close();
EVP_CIPHER_CTX_free(ctx);
return -1;
}
// 读取输入文件并解密
const int BUFSIZE = 1024 * 1024 + EVP_CIPHER_block_size(EVP_aes_256_cbc());
unsigned char inbuf[BUFSIZE];
unsigned char outbuf[BUFSIZE];
int inlen, outlen;
while (!fin.eof()) {
fin.read((char *)inbuf, BUFSIZE);
inlen = fin.gcount();
if (EVP_DecryptUpdate(ctx, outbuf, &outlen, inbuf, inlen) != 1) {
cerr << "Error: EVP_DecryptUpdate() failed" << endl;
fin.close();
fout.close();
EVP_CIPHER_CTX_free(ctx);
return -1;
}
fout.write((char *)outbuf, outlen);
}
// 结束解密
if (EVP_DecryptFinal_ex(ctx, outbuf, &outlen) != 1) {
cerr << "Error: EVP_DecryptFinal_ex() failed" << endl;
fin.close();
fout.close();
EVP_CIPHER_CTX_free(ctx);
return -1;
}
fout.write((char *)outbuf, outlen);
// 关闭文件和解密上下文
fin.close();
fout.close();
EVP_CIPHER_CTX_free(ctx);
return 0;
}
int main()
{
const char *key = "0123456789abcdef0123456789abcdef";
const char *iv = "0123456789abcdef";
const char *infile = "input.txt";
const char *outfile = "output.txt";
// 加密
if (aes_encrypt(key, iv, infile, outfile) != 0) {
cerr << "Error: Encryption failed" << endl;
return -1;
}
cout << "Encryption succeeded" << endl;
// 解密
if (aes_decrypt(key, iv, outfile, "input_decrypted.txt") != 0) {
cerr << "Error: Decryption failed" << endl;
return -1;
}
cout << "Decryption succeeded" << endl;
return 0;
}
```
这个示例代码中,我们使用了OpenSSL库提供的EVP_aes_256_cbc加密算法,对给定的输入文件进行加密和解密。在实际使用中,你需要根据自己的需求修改参数,比如加密算法、密钥、向量、输入文件和输出文件等。
阅读全文