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;
}
相关推荐
晚风(●•σ )4 分钟前
C++语言程序设计——11 C语言风格输入/输出函数
c语言·开发语言·c++
恒者走天下1 小时前
秋招落定,拿到满意的offer,怎么提高自己实际的开发能力,更好的融入团队
c++
天若有情6732 小时前
【c++】手撸C++ Promise:从零实现通用异步回调组件,支持链式调用+异常安全
开发语言·前端·javascript·c++·promise
学困昇2 小时前
C++中的异常
android·java·c++
合作小小程序员小小店3 小时前
桌面安全开发,桌面二进制%恶意行为拦截查杀%系统安全开发3.0,基于c/c++语言,mfc,win32,ring3,dll,hook,inject,无数据库
c语言·开发语言·c++·安全·系统安全
Codeking__3 小时前
C++ 11 atomic 原子性操作
开发语言·c++
crescent_悦3 小时前
PTA L1-020 帅到没朋友 C++
数据结构·c++·算法
卡提西亚3 小时前
C++笔记-34-map/multimap容器
开发语言·c++·笔记
2***B4494 小时前
C++在金融中的QuantLibXL
开发语言·c++·金融
A***07174 小时前
C++在游戏中的阴影渲染
开发语言·c++·游戏