You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用CryptoPP移植C# AES解密代码到C++时输出乱码问题

AES解密跨实现乱码问题排查

原C#实现的隐式默认参数

翻译代码时未显式对齐C# Aes 类的默认配置是乱码的核心诱因,C#端固定参数如下:

  • 工作模式:CipherMode.CBC
  • 填充模式:PaddingMode.PKCS7
  • 密钥派生逻辑:Rfc2898DeriveBytes 即PBKDF2-HMAC-SHA1,迭代100次、盐值为IV,这部分现有C++实现逻辑正确。

现有C++代码的具体错误

  • 工作模式不匹配:代码中使用CTR_Mode<AES>做解密,和C#默认的CBC模式完全不兼容,对称加密模式不匹配时解密结果无意义,必然输出乱码。
  • 密文读取逻辑缺陷:逐字节循环仅读取最多bufSize长度的密文,无法处理大于缓冲区的文件;且未正确处理文件尾状态,会丢失末尾密文数据。
  • 流处理逻辑缺失:原C#实现为流式逐块解密,支持任意大小文件;现有实现尝试将密文全量加载到内存解密,大文件场景会触发内存溢出,也未对齐原逻辑的流式处理行为。

修正后的实现代码

#include <cryptopp/aes.h>
#include <cryptopp/modes.h>
#include <cryptopp/pwdbased.h>
#include <cryptopp/sha.h>
#include <cryptopp/filters.h>
#include <cryptopp/files.h>
#include <fstream>
#include <string_view>
#include <stdexcept>

void decrypt(std::ifstream& in_file, std::ofstream& out_file, std::string_view password, size_t bufSize) {
    using namespace CryptoPP;
    // 读取16字节IV
    byte iv[AES::BLOCKSIZE];
    in_file.read(reinterpret_cast<char*>(iv), sizeof(iv));
    if (in_file.gcount() != sizeof(iv)) {
        throw std::runtime_error("IV读取失败,文件格式非法");
    }

    // 派生16字节AES密钥,完全对齐C# Rfc2898DeriveBytes逻辑
    byte key[AES::DEFAULT_KEYLENGTH];
    PKCS5_PBKDF2_HMAC<SHA1> pbkdf2;
    pbkdf2.DeriveKey(
        key, sizeof(key),
        0,
        reinterpret_cast<const byte*>(password.data()), password.size(),
        iv, sizeof(iv),
        100
    );

    // 使用CBC模式+默认PKCS7填充,完全匹配C#默认配置
    CBC_Mode<AES>::Decryption decryptor;
    decryptor.SetKeyWithIV(key, sizeof(key), iv);

    // 流式解密:边读入密文边解密边写入输出,对齐C# CryptoStream处理逻辑
    FileSource(
        in_file,
        true,
        new StreamTransformationFilter(
            decryptor,
            new FileSink(out_file)
        ),
        false,
        bufSize
    );
}

注意:调用函数前必须以std::ios::binary二进制模式打开输入、输出文件,否则Windows平台会自动做换行符转换,损坏密文/明文数据。

内容的提问来源于stack exchange,提问作者tuxifan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 15:42:22