代码之家  ›  专栏  ›  技术社区  ›  Chen Kinnrot

我的三重DES包装有什么问题?

  •  2
  • Chen Kinnrot  · 技术社区  · 15 年前

    在调用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();
        }
    }
    
    4 回复  |  直到 15 年前
        1
  •  2
  •   Michael Howard-MSFT    15 年前

    您的代码没有添加6个字节,3des被四舍五入为一个完整的块大小。这就是分组密码的工作原理。您只能在得到的密文中看到,而不能在解密的明文中看到。

    同样,无需担心,块密码必须将明文填充到下一个块大小,然后才能加密明文。解密密文时,填充将被删除。

    另外,我做了一个快速的代码检查,代码中有一个错误——您对IV和密钥使用的是同一个密钥,您真的应该使用不同的数据。因此,我将添加另一个arg到decryptfile()、encryptfile()和setkeys(),以允许使用不同的IV。

        2
  •  0
  •   Juri Robl    15 年前

    我不知道您使用哪种加密模式,但是ecb和cbc模式在最后一个块的末尾添加了填充位,以将其大小增加到64位。也许这就是你字节的来源?

        3
  •  0
  •   Jonas Elfström    15 年前

    tiple des是一个64位的分组密码。在不分析代码的情况下,我猜想您的数据不是64位对齐的,并且已经用 default PKCS7 mode .

        4
  •  0
  •   mafu    15 年前

    我浏览了一下代码,发现没有问题,但是我对你使用的类不太熟悉,所以我可能错了。相反,我将给出一些适用于 任何 缺陷:

    试着找出问题所在。在许多代码行中很难发现错误。创建显示错误行为的尽可能短的代码。

    例如,尝试将字符串写入输出流。尺寸正确吗?

    • 如果没有,你几乎发现了问题——文件写入有问题,加密与问题无关。
    • 如果是,继续以小步骤添加功能,直到发现问题为止。

    这是一种标准的调试技术,非常有用,我建议每次遇到问题时都使用它。

    除此之外,尝试始终保持干净的启动条件,即确保写入的文件已被删除。在不同的输入上尝试您的代码,看看它是否每次都会以完全相同的结果失败,或者是否存在差异。

    编辑:

    根据其他响应,64位块导致了您的问题。有了前面描述的思想,您可以很容易地排除许多其他因素,直到只剩下加密本身。

    然后,您可以问“为什么Tripedes会向输入添加多达7个字节?”哪个问题比“我的三重DES包装有什么问题”更清楚??“-你肯定会在不到一分钟的时间内得到这个简单问题的答案!