MFC: 文件加解密(单元测试模块)

背景:

  1. 对敏感配置文件(如 XML 格式的配置文件、用户信息等)进行加密,防止被人以文本形式直接查看。
  2. 软件启动前加载加密的配置或资源文件,运行时再进行解密使用,提高逆向破解门槛。
  3. 在传输 XML 文件(如通过网络发送)前进行加密,保障数据在传输过程中的安全性。
cpp 复制代码
#include <openssl/aes.h>
#include <stdio.h>
#include <stdlib.h>

void AESEncrypt(const unsigned char *inputData, size_t dataSize, unsigned char *outputData, const unsigned char *key) {
    AES_KEY aesKey;
    AES_set_encrypt_key(key, 256, &aesKey);

    size_t numBlocks = dataSize / 16;

    for (size_t i = 0; i < numBlocks; ++i) {
        AES_ecb_encrypt(inputData + i * 16, outputData + i * 16, &aesKey, AES_ENCRYPT);
    }
}

void AESDecrypt(const unsigned char *inputData, size_t dataSize, unsigned char *outputData, const unsigned char *key) {
    AES_KEY aesKey;
    AES_set_decrypt_key(key, 256, &aesKey);

    size_t numBlocks = dataSize / 16;

    for (size_t i = 0; i < numBlocks; ++i) {
        AES_ecb_encrypt(inputData + i * 16, outputData + i * 16, &aesKey, AES_DECRYPT);
    }
}

int main() {
   unsigned char key[32] = "1234567890abcdef1234567890abcdef";  // 32 字节

    // Read the XML file
    const char *filePath = "path_to_your_xml_file.xml";
    FILE *file = fopen(filePath, "rb");
    if (!file) {
        perror("File open error");
        return 1;
    }

    fseek(file, 0, SEEK_END);
    long fileSize = ftell(file);
    fseek(file, 0, SEEK_SET);

    unsigned char *originalData = (unsigned char *)malloc(fileSize);
    fread(originalData, 1, fileSize, file);
    fclose(file);

    // Allocate memory for encrypted data
    unsigned char *encryptedData = (unsigned char *)malloc(fileSize);

    AESEncrypt(originalData, fileSize, encryptedData, key);

    // Write encrypted data back to the file
    file = fopen(filePath, "wb");
    if (!file) {
        perror("File open error");
        return 1;
    }
    fwrite(encryptedData, 1, fileSize, file);
    fclose(file);

    // Clean up
    free(originalData);
    free(encryptedData);

    return 0;
}
相关推荐
卡提西亚8 分钟前
C++笔记-21-运算符重载
c++·笔记
草莓熊Lotso24 分钟前
C++ 继承特殊场景解析:友元、静态成员与菱形继承的底层逻辑
服务器·开发语言·c++·人工智能·经验分享·笔记·1024程序员节
利刃大大44 分钟前
【动态规划:01背包】01背包详解 && 模板题 && 优化
c++·算法·动态规划·力扣·背包问题
9ilk1 小时前
【基于one-loop-per-thread的高并发服务器】--- 前置技术
运维·服务器·c++·笔记·后端·中间件
苏比的博客3 小时前
Windows MFC添加类,变量,类导向
c++·windows·mfc
yudiandian20143 小时前
MFC - 使用 Base64 对图片进行加密解密
c++·mfc
yudiandian20143 小时前
MFC - Picture Control 控件显示图片
c++·mfc
我是李武涯8 小时前
从`std::mutex`到`std::lock_guard`与`std::unique_lock`的演进之路
开发语言·c++
卡提西亚8 小时前
C++笔记-10-循环语句
c++·笔记·算法
亮剑20189 小时前
第1节:C语言初体验——环境、结构与基本数据类型
c++