#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/aes.h>
#define KEY_SIZE 16
void decryptFile(const char* inputFile, const char* outputFile, const unsigned char* key) {
FILE* inFile = fopen(inputFile, "rb");
FILE* outFile = fopen(outputFile, "wb");
if (inFile == NULL || outFile == NULL) {
printf("无法打开文件\n");
return;
}
// 读取文件内容
fseek(inFile, 0, SEEK_END);
long fileSize = ftell(inFile);
rewind(inFile);
unsigned char* fileContent = (unsigned char*)malloc(fileSize);
fread(fileContent, sizeof(unsigned char), fileSize, inFile);
// 解密
AES_KEY aesKey;
AES_set_decrypt_key(key, KEY_SIZE * 8, &aesKey);
unsigned char iv[AES_BLOCK_SIZE] = {0}; // 初始化向量
unsigned char decryptedContent[fileSize];
AES_cbc_encrypt(fileContent, decryptedContent, fileSize, &aesKey, iv, 0); // decrypt参数设置为0
// 将解密后的内容写入文件
fwrite(decryptedContent, sizeof(unsigned char), fileSize, outFile);
// 释放资源
free(fileContent);
fclose(inFile);
fclose(outFile);
printf("解密完成\n");
}
int main() {
const char* inputFile = "encrypted.txt"; // 加密后的文件名
const char* outputFile = "decrypted.txt"; // 解密后的文件名
unsigned char key[KEY_SIZE] = "aa12345678"; // 密钥
decryptFile(inputFile, outputFile, key);
return 0;
}