gpt4 book ai didi

C#加密,C++解密。最后几个字节解密失败

转载 作者:行者123 更新时间:2023-11-30 02:12:31 24 4
gpt4 key购买 nike

对于我即将列出的代码的长度,我深表歉意。

我需要在代码的 C# 端加密 xml 文件的内容,然后在 C++ 中解密。我正在使用 RC2,RC2CryptoServiceProviderCryptoStream在 C# 端,使用 Wincrypt在 C++ 方面。加密似乎工作正常,看起来像这样:

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();
}

我的解密代码几乎完美运行。它缺少文件的最后两个字符,而是吐出垃圾字符。我试过用 null 终止解密的字符串,但没有骰子。具体如下:

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 结尾

为什么会这样?我怎样才能阻止这个?我对为什么会这样感到非常困惑。由于我是 null 终止解密并且仍然只在最后一个字符上遇到问题,所以我不认为解密是问题所在(尽管我很想错)。完成文件加密时,我的加密是否有问题?

从 CryptDecrypt 返回后,dwCount 等于 size,即 11296。同时,originalSize 等于 11290。

最佳答案

这一行是你的问题(在调用 CryptDecrypt 之前):

somebytes[originalSize - 1] = '\0';

它用零覆盖最后一个 block 中的部分加密填充,这导致最后一个 block 解密为垃圾。只需删除该行 - 密文无论如何都不是 nul 终止的(它几乎肯定包含大量嵌入的 nul),解密例程使用长度参数来了解有多少数据。

哦,还有……RC2?认真的吗?

关于C#加密,C++解密。最后几个字节解密失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1627045/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com