在 .NET 中实现 en/decrypting 流的正确方法



我想导入和导出旧的游戏文件格式,并且其数据已加密。详细信息可在此处找到;简而言之,该文件被分成多个块,每个块都使用基于前一个 uint 的特定类型的 XOR 加密,并且校验和跟踪我在读取数据时需要跳过的每个块。

通常,我想设计放置在游戏文件上的流是可重用的,如果有一个流在后台进行加密/解密,而开发人员只是与BinaryReader/Writer一起工作来做一些ReadUInt32()的东西等,那就太好了。

所以我到目前为止研究过 .NET 中有一个CryptoStream类,实现 en/decryption 的"正确"方法会从继承该类开始吗?我没有找到关于以这种方式尝试过的人的文章,因此我不确定我在这里是否完全错了。

虽然不是 C#,但此 MSDN 页可能会提供一些见解,显示 ICryptoTransform 接口的实现。

下面是一个示例,说明它在 C# 中的外观,您在用例中提到了带有前一个块的 XOR(毫无疑问,您必须调整它以匹配您的确切算法):

using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
class XORCryptoTransform : ICryptoTransform
{
    uint previous;
    bool encrypting;
    public XORCryptoTransform(uint iv, bool encrypting)
    {
        previous = iv;
        this.encrypting = encrypting;
    }
    public int TransformBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
    {
        for (int i = 0; i < inputCount; i+=4)
        {
            uint block = BitConverter.ToUInt32(inputBuffer, inputOffset + i);
            byte[] transformed = BitConverter.GetBytes(block ^ previous);
            Array.Copy(transformed, 0, outputBuffer, outputOffset + i, Math.Min(transformed.Length, outputBuffer.Length - outputOffset -i));
            if (encrypting)
            {
                previous = block;
            }
            else
            {
                previous = BitConverter.ToUInt32(transformed, 0);
            }
        }
        return inputCount;
    }
    public byte[] TransformFinalBlock(byte[] inputBuffer, int inputOffset, int inputCount)
    {
        var transformed = new byte[inputCount];
        TransformBlock(inputBuffer, inputOffset, inputCount, transformed, 0);
        return transformed;
    }
    public bool CanReuseTransform
    {
        get { return true; }
    }
    public bool CanTransformMultipleBlocks
    {
        get { return true; }
    }
    public int InputBlockSize
    {
        // 4 bytes in uint
        get { return 4; }
    }
    public int OutputBlockSize
    {
        get { return 4; }
    }
    public void Dispose()
    {
    }
}
class Program
{
    static void Main()
    {
        uint iv = 0; // first block will not be changed
        byte[] plaintext = Guid.NewGuid().ToByteArray();
        byte[] ciphertext;
        using (var memoryStream = new MemoryStream())
        {
            using (var encryptStream = new CryptoStream(
                memoryStream,
                new XORCryptoTransform(iv, true),
                CryptoStreamMode.Write))
            {
                encryptStream.Write(plaintext, 0, plaintext.Length);
            }
            ciphertext = memoryStream.ToArray();
        }
        byte[] decrypted = new byte[ciphertext.Length];
        using (var memoryStream = new MemoryStream(ciphertext))
        using (var encryptStream = new CryptoStream(
                memoryStream,
                new XORCryptoTransform(iv, false),
                CryptoStreamMode.Read))
        {
            encryptStream.Read(decrypted, 0, decrypted.Length);
        }
        bool matched = plaintext.SequenceEqual(decrypted);
        Console.WriteLine("Matched: {0}", matched);
    }
}

在此示例中,如果输入数据是块长度的倍数(在您的情况下,uint为 4 个字节),则在 TransformFinalBlock 中无需执行任何操作。但是,如果数据不是块长度的倍数,则剩余字节将在那里处理。

.NET 会自动用零填充传递给 TransformFinalBlock 的数组,使其达到块长度,但您可以通过检查inputCount(这将是实际输入长度,而不是填充长度)并替换为你自己的自定义(非零)填充(如果您的算法需要)来检测这一点。

不,从CryptoStream继承不是正确的做法。如果你想走这条路,正确的方法是创建一个实现ICryptoTransform的类,并将解密和加密逻辑放在那里。然后,将 ICryptoTransform 类作为参数传递给 CryptoStream

相关内容

最新更新