对于我即将列出的代码长度,我深表歉意。
我需要在我的代码的 C# 端加密一个 xml 文件的内容,并在 C++ 中解密它。我在 C# 端和RC2CryptoServiceProvider
C ++ 端使用 RC2。加密似乎工作正常,看起来像这样:CryptoStream
Wincrypt
public static byte[] EncryptString(byte[] input, string password)
{
PasswordDeriveBytes pderiver = new PasswordDeriveBytes(password, null);
byte[] ivZeros = new byte[8];
byte[] pbeKey = pderiver.CryptDeriveKey("RC2", "MD5", 128, ivZeros);
RC2CryptoServiceProvider RC2 = new RC2CryptoServiceProvider();
byte[] IV = new byte[8];
ICryptoTransform encryptor = RC2.CreateEncryptor(pbeKey, IV);
MemoryStream msEncrypt = new MemoryStream();
CryptoStream csEncrypt = new CryptoStream(msEncrypt, encryptor, CryptoStreamMode.Write);
csEncrypt.Write(input, 0, input.Length);
csEncrypt.FlushFinalBlock();
return msEncrypt.ToArray();
}
我的解密代码几乎可以完美运行。它缺少文件的最后两个字符,而是吐出垃圾字符。我已经尝试对解密的字符串进行空终止,但没有骰子。如下:
char* FileReader::DecryptMyFile(char *input, char *password, int size, int originalSize)
{
UNREFERENCED_PARAMETER(password);
HCRYPTPROV provider = NULL;
if(CryptAcquireContext(&provider, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0))
{
printf("Context acquired.");
}
else
{
if (GetLastError() == NTE_BAD_KEYSET)
{
if(CryptAcquireContext(&provider, 0, NULL, PROV_RSA_FULL, CRYPT_NEWKEYSET))
{
printf("new key made.");
}
else
{
printf("Could not acquire context.");
}
}
else
{
DWORD check = GetLastError();
UNREFERENCED_PARAMETER(check);
printf("Could not acquire context.");
}
}
HCRYPTKEY key = NULL;
HCRYPTHASH hash = NULL;
if(CryptCreateHash(provider, CALG_MD5, 0, 0, &hash))
{
printf("empty hash created.");
}
if(CryptHashData(hash, (BYTE *)password, strlen(password), 0))
{
printf("data buffer is added to hash.");
}
HCRYPTHASH duphash = NULL;
CryptDuplicateHash(hash, 0, 0, &duphash);
BYTE *mydata = new BYTE[512];
DWORD mydatasize = 512;
CryptGetHashParam(hash, HP_HASHVAL, mydata, &mydatasize, 0);
BYTE *mydata2 = new BYTE[512]; //these duplicates were made to test my hash.
DWORD mydatasize2 = 512;
CryptGetHashParam(duphash, HP_HASHVAL, mydata2, &mydatasize2, 0);
if(CryptDeriveKey(provider, CALG_RC2, hash, 0, &key))
{
printf("key derived.");
}
DWORD dwKeyLength = 128;
if(CryptSetKeyParam(key, KP_EFFECTIVE_KEYLEN, reinterpret_cast<BYTE*>(&dwKeyLength), 0))
{
printf("CryptSetKeyParam success");
}
BYTE IV[8] = {0,0,0,0,0,0,0,0};
if(CryptSetKeyParam(key, KP_IV, IV, 0))
{
printf("CryptSetKeyParam worked");
}
DWORD dwCount = size;
BYTE *somebytes = new BYTE[dwCount + 1];
memcpy(somebytes, input, dwCount);
if(CryptDecrypt(key,0, true, 0, somebytes, &dwCount))
{
printf("CryptDecrypt succeeded.");
}
else
{
if(GetLastError() == NTE_BAD_DATA)
{
printf("NTE_BAD_DATA");
}
printf("CryptDecrypt failed.");
DWORD testest = NULL;
testest = GetLastError();
testest = 3;
}
somebytes[originalSize] = '\0';
return (char *)somebytes;
}
生成的 xml 文件应以</LudoData>
. 目前,它以</LudoDat[funny looking s]b
为什么会这样?我怎样才能阻止这个?我对为什么会发生这种情况感到非常困惑。由于我以空值终止解密并且仅在最终字符上遇到问题,因此我不认为解密是问题(尽管我很想错)。完成文件加密时,我的加密可能有问题吗?
从 CryptDecrypt 返回时,dwCount 等于 size,即 11296。同时, originalSize 等于 11290。