使用Windows AES加密提供程序在C++中解密C#加密数据

3

我需要使用RijndaelManaged在C#中加密数据,并在C++代码中解密。

C#加密代码:

static string Encrypt(string plainText)
{
    byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);

    var keyBytes = new byte[] { /* ... 32 bytes of a key */};
    byte[] iv = new byte[] { /* ... 16 bytes of IV */ };


    var symmetricKey = new RijndaelManaged() 
    { 
        Mode = CipherMode.CBC, 
        Padding = PaddingMode.Zeros, 
        BlockSize = 128, // Must be 128 to be compatible with AES
        KeySize = 256 
    };

    var encryptor = symmetricKey.CreateEncryptor(keyBytes, iv);

    byte[] cipherTextBytes;
    using(var memoryStream = new MemoryStream())
    {
        using(var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
        {
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.FlushFinalBlock();
            cipherTextBytes = memoryStream.ToArray();
            cryptoStream.Close();
        }
        memoryStream.Close();
    }
    return Convert.ToBase64String(cipherTextBytes);
}

但是,当我在C++代码中尝试解密时,总是会收到CryptDecrypt的NTE_BAD_DATA响应。以下是C++代码(为了清晰起见,所有检查都已删除):
__declspec(dllexport) DWORD  Decrypt(char* stringBuffer)
{
string encryptedString(stringBuffer);

// Decode base64 string to byte array. Works ok, the binary array is the same as the one in C# code.
vector<BYTE> encryptionBuffer = Base64::decode(encryptedString);
DWORD bufferSize = encryptionBuffer.size();

struct CryptoBlob {
    BLOBHEADER header;
    DWORD cbKeySize;
    BYTE rgbKeyData[32];
} keyBlob;

keyBlob.header.bType = PLAINTEXTKEYBLOB;
keyBlob.header.bVersion = CUR_BLOB_VERSION;
keyBlob.header.reserved = 0;
keyBlob.header.aiKeyAlg = CALG_AES_256;
keyBlob.cbKeySize = 32;

BYTE keyData[32] = { /* 32 bytes of a key the same as in C# code */ };
BYTE ivData[16] = { /* 16 bytes of IV the same as in C# code */ };

memcpy(keyBlob.rgbKeyData, keyData, 32);

HCRYPTKEY hPubKey;
HCRYPTPROV hProv;

CryptAcquireContext(
    &hProv,
    NULL,
    NULL,
    PROV_RSA_AES,
    CRYPT_VERIFYCONTEXT);

CryptImportKey(hProv, (const LPBYTE)&keyBlob, sizeof(keyBlob), 0, 0, &hPubKey);
CryptSetKeyParam(hPubKey, KP_IV, ivData, 0);

// Here the error happens, the value returned is 0x80090005 (NTE_BAD_DATA)
DWORD err = CryptDecrypt(hPubKey, 0, TRUE, 0, encryptionBuffer.data(), &bufferSize);

// overwrite the input buffer with decrypted data
memset(stringBuffer, 0, encryptedString.length());
memcpy(stringBuffer, encryptionBuffer.data(), bufferSize);

return 0;
}

有什么想法可能出了问题吗? 谢谢!

1
我看到了几个关于padding问题的参考资料,也许值得研究。请查看这里:CryptoAPI CryptDecrypt function NT_BAD_DATA error 和这里:CryptDecrypt function(注意NTE_BAD_DATA错误代码的注释)。 - Phil Brubaker
2个回答

5
当你将TRUE作为第三个参数传递给CryptDecrypt时,它会尝试撤销PKCS#7填充。当无法撤消该填充时,它会发出NTE_BAD_DATA错误。
由于你已将加密的填充模式更改为Pkcs7以外的值,所以需要传递FALSE并执行手动去除填充。
由于PaddingMode.Zeros本质上是不可逆的,因此不需要执行去除填充的操作。

0
如果上述不是答案,我建议查看C++和C#中的密钥/iv,并确保字节数组完全相同。
在末尾添加额外的字符可能会导致问题。
如果它们不匹配,请注意,编程语言之间和跨实现之间可能存在返回输出类型(例如有符号/无符号,char/字节数组)的差异,这也可能会导致问题。

是的,那个表述非常不清楚。我已经进行了编辑,感谢您的评论。 - didiz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接