CryptopgraphicException When Trying to Unprotect Byte Array”

本文关键字:Byte Array Unprotect to When Trying CryptopgraphicException | 更新日期: 2023-09-27 18:00:41

好吧,我已经尝试加密和解密文件一段时间了,我一直得到这个异常

System.Security.Cryptography.CryptographicException:参数不正确。

它完美地写入了文件,字节数组确实有合法的条目。但当我尝试解密时,仍然会抛出这个。

这是整个功能:

    private static void loadTimes()
    {
        try
        {
            short[] encShorts = new short[bestTimes.Length / 2];
            IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream stream = new IsolatedStorageFileStream("test.txt", FileMode.Open, FileAccess.ReadWrite, store);
            StreamReader stmReader = new StreamReader(stream);
            int i = 0;
            while (!stmReader.EndOfStream)
            {
                encShorts[i] = short.Parse(stmReader.ReadLine());
                i++;
            }
            stmReader.Close();
            byte[] encBytes = new byte[bestTimes.Length];
            for (int j = 0; j < encShorts.Length; j++)
            {
                encBytes[j * 2] = (byte)(encShorts[j] / 256);
                encBytes[j * 2 + 1] = (byte)(encShorts[j] % 256);
            }
            bestTimes = ProtectedData.Unprotect(encBytes, null);
            checkForTimeAds();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.ToString());
        }
    }

基本上,它是从一个文件中加载以获得游戏中的最佳时间分数,由于它们是短裤,我将它们分成两部分。

以下代码引发异常:

bestTimes = ProtectedData.Unprotect(encBytes, null);

我到处看了看,似乎很多人都没有解决这个问题,有些人说"种族状况",但我不完全确定这是否适用于这里。为什么我会得到这个例外?

Jon Skeet请求的保存代码:

    private static void saveTimes()
    {
        try
        {
            byte[] encResult = ProtectedData.Protect(bestTimes, null);
            short[] encShorts = new short[bestTimes.Length / 2];
            for (int i = 0; i < encShorts.Length; i++)
            {
                encShorts[i] = (short) (encResult[i] * 256 + encResult[i + 1]);
            }
            IsolatedStorageFile store = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
            IsolatedStorageFileStream stream = new IsolatedStorageFileStream("test.txt", FileMode.Create, FileAccess.Write, store);
            StreamWriter writer = new StreamWriter(stream);
            foreach (short part in encShorts)
            {
                writer.WriteLine(part);
            }
            writer.Close();
        }
        catch (Exception)
        {
            MessageBox.Show("Couldn't save the file.");
        }
        checkForTimeAds();
    }

CryptopgraphicException When Trying to Unprotect Byte Array”

您的原始转换代码(到short[])已损坏:

short[] encShorts = new short[bestTimes.Length / 2];
for (int i = 0; i < encShorts.Length; i++)
{
    encShorts[i] = (short) (encResult[i] * 256 + encResult[i + 1]);
}

应该是:

short[] encShorts = new short[encResult.Length / 2];
for (int i = 0; i < encShorts.Length; i++)
{
    encShorts[i] = (short) (encResult[i * 2] * 256 + encResult[i * 2 + 1]);
}

请注意,在确定长度时使用encResult而不是bestTimes,并且在访问encResult时使用加倍的i

此外,如果encResult的字节数为奇数,那么您还没有考虑最后一个字节。

从根本上讲,不清楚为什么要将byte[]转换为short[],然后将其作为文本写入磁盘。如果你避免了所有这些转换,只写出原始字节,你就不会有这个问题。