我試圖解密之前用C#TripleDESCryptoServiceProvider加密的非託管C++中的文件。不幸的是,我不知道如何使用Microsoft Crypt API(advapi32.lib)來做到這一點。下面是我用來加密數據的C#代碼:在C++中使用用Crypt API加密的C++中的TripleDES解密文件
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();
}
正如你所看到的密鑰和IV是相當簡單的(只是用於測試目的)。所以我的問題是,如何在C++中解密該文件?我知道TripleDESCryptoServiceProvider只是Crypt API的包裝器,所以解決這個問題不是那麼困難。
有沒有人做過這樣的事情,可以幫助我?
Thx Simon
你可以使用C++ w/.net運行時? (我敢打賭不,你很可能使用C++,所以你不需要在你的解密應用程序上安裝.net) – 2010-01-29 21:16:46