c# - 我的三重 DES 包装程序有什么问题?

标签 c# .net encryption cryptography

似乎我的代码在调用 encrypt decrypt 后向结果文件添加了 6 个字节。 我在 mkv 文件上试了一下.. 请帮忙

这是我的代码

class TripleDESCryptoService : IEncryptor, IDecryptor
{
    public void Encrypt(string inputFileName, string outputFileName, string key)
    {
        EncryptFile(inputFileName, outputFileName, key);
    }

    public void Decrypt(string inputFileName, string outputFileName, string key)
    {
        DecryptFile(inputFileName, outputFileName, key);
    }

    static void EncryptFile(string inputFileName, string outputFileName, string sKey)
    {
        var outFile = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);

        // The chryptographic service provider we're going to use
        var cryptoAlgorithm = new TripleDESCryptoServiceProvider();
        SetKeys(cryptoAlgorithm, sKey);

        // This object links data streams to cryptographic values
        var cryptoStream = new CryptoStream(outFile, cryptoAlgorithm.CreateEncryptor(), CryptoStreamMode.Write);

        // This stream writer will write the new file
        var encryptionStream = new BinaryWriter(cryptoStream);

        // This stream reader will read the file to encrypt
        var inFile = new FileStream(inputFileName, FileMode.Open, FileAccess.Read);
        var readwe = new BinaryReader(inFile);

        // Loop through the file to encrypt, line by line
        var date = readwe.ReadBytes((int)readwe.BaseStream.Length);


        // Write to the encryption stream
        encryptionStream.Write(date);


        // Wrap things up
        inFile.Close();
        encryptionStream.Flush();
        encryptionStream.Close();
    }

    private static void SetKeys(SymmetricAlgorithm algorithm, string key)
    {
        var keyAsBytes = Encoding.ASCII.GetBytes(key);
        algorithm.IV = keyAsBytes.Take(algorithm.IV.Length).ToArray();
        algorithm.Key = keyAsBytes.Take(algorithm.Key.Length).ToArray();
    }

    static void DecryptFile(string inputFilename, string outputFilename, string sKey)
    {
        // The encrypted file
        var inFile = File.OpenRead(inputFilename);

        // The decrypted file
        var outFile = new FileStream(outputFilename, FileMode.OpenOrCreate, FileAccess.ReadWrite);

        // Prepare the encryption algorithm and read the key from the key file
        var cryptAlgorithm = new TripleDESCryptoServiceProvider();

        SetKeys(cryptAlgorithm, sKey);

        // The cryptographic stream takes in the encrypted file
        var encryptionStream = new CryptoStream(inFile, cryptAlgorithm.CreateDecryptor(), CryptoStreamMode.Read);

        // Write the new unecrypted file
        var cleanStreamReader = new BinaryReader(encryptionStream);
        var cleanStreamWriter = new BinaryWriter(outFile);
        cleanStreamWriter.Write(cleanStreamReader.ReadBytes((int)inFile.Length));
        cleanStreamWriter.Close();
        outFile.Close();
        cleanStreamReader.Close();
    }
}

最佳答案

您的代码没有添加六个字节,3DES 正在四舍五入到完整的 block 大小。这就是分组密码的工作原理。您只会在生成的密文中看到这一点,而不会在解密的明文中看到。

再次无需担心, block 密码必须先将您的明文填充到下一个 block 大小,然后才能加密明文。当您解密密文时,填充将被删除。

此外,我进行了快速代码审查,发现您的代码有错误 - 您对 IV 和 key 使用了相同的 key ,而您确实应该使用不同的数据。因此,我会向 DecryptFile()、EncryptFile() 和 SetKeys() 添加另一个 arg 以允许不同的 IV。

关于c# - 我的三重 DES 包装程序有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2545580/

相关文章:

c# - 在 C# 中从 WM_MOUSEHWHEEL 消息中的 wParam 检索 WHEEL_DELTA

c# - Roslyn future 分支不能使用 C# 7 功能元组 - 错误 CS0518 预定义类型 'System.ValueTuple` 2

c# - 我在尝试获取当前位置时得到 NAN - 经度和纬度

c# - 指针作为泛型 C#

javascript - 是否可以在 Php 中加密 JWT token 并在 Javascript 中解密?

encryption - 使用 GPU 进行加密硬件加速

c# - 通过 TCP 发送大对象 : "End of Stream encountered before parsing was completed"

c# - 在等待新线程产生时调用 thread.sleep()

c# BackgroundWorker DoWork 方法调用另一个类和 ProgressReport

javascript - 客户端敏感数据加密