我正在尝试解密以前使用 C# TripleDESCryptoServiceProvider 加密的非托管 C++ 文件。不幸的是,我不知道如何使用 Microsoft Crypt API (advapi32.lib) 来做到这一点。这是我用来加密数据的 C# 代码:
private static void EncryptData(MemoryStream streamToEncrypt)
{
// initialize the encryption algorithm
TripleDES algorithm = new TripleDESCryptoServiceProvider();
byte[] desIV = new byte[8];
byte[] desKey = new byte[16];
for (int i = 0; i < 8; ++i)
{
desIV[i] = (byte)i;
}
for (int j = 0; j < 16; ++j)
{
desKey[j] = (byte)j;
}
FileStream outputStream = new FileStream(TheCryptedSettingsFilePath, FileMode.OpenOrCreate, FileAccess.Write);
outputStream.SetLength(0);
CryptoStream encStream = new CryptoStream(outputStream, algorithm.CreateEncryptor(desKey, desIV),
CryptoStreamMode.Write);
// write the encrypted data to the file
encStream.Write(streamToEncrypt.ToArray(), 0, (int)streamToEncrypt.Length);
encStream.Close();
outputStream.Close();
}
如您所见,Key 和 IV 非常简单(仅用于测试目的)。所以我的问题是,如何用 C++ 解密该文件?我知道 TripleDESCryptoServiceProvider 只是 Crypt API 的包装器,所以解决这个问题并没有那么困难。
有没有人做过类似的事情并且可以帮助我?
谢谢西蒙
最佳答案
不幸的是,非托管 CAPI (advapi32.lib) 需要分配比 System.Security.Cryptography 命名空间所能完成的代码更多的代码。 MSDN 有一个 CAPI 示例,名为“Decrypting a File ”,它显示了实现您在测试应用程序中尝试执行的操作所需的所有步骤和调用。这对你来说可能是一个很好的起点。很抱歉没有发布可用的代码来玩,但是当你看一下示例代码时你就会明白为什么。
关于c# - 使用在 C# 中使用 TripleDES 加密的 Microsoft Crypt API 在 C++ 中解密文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2155678/