Blowfish无效长度异常

时间:2012-08-27 14:22:17

标签: c# blowfish

我正在使用Blowfish算法加密我的文件,但似乎我对此一无所知。每当我尝试Encipher()时,它都会抛出一个说“无效长度”的异常。我认为当它用8获取mod时长度必须为零,我认为这意味着应该有8乘8个流来启动加密。我该怎么办?

Blowfish的加密方法:

public void Encipher(byte[] data, int length)
{
    uint xl, xr;
    if ((length % 8) != 0) <-- Exception Line
        throw new Exception("Invalid Length");
    for (int i = 0; i < length; i += 8)
    {
        // Encode the data in 8 byte blocks.
        xl = (uint)((data[i] << 24) | (data[i + 1] << 16) | (data[i + 2] << 8) | data[i + 3]);
        xr = (uint)((data[i + 4] << 24) | (data[i + 5] << 16) | (data[i + 6] << 8) | data[i + 7]);
        Encipher(ref xl, ref xr);
        // Now Replace the data.
        data[i] = (byte)(xl >> 24);
        data[i + 1] = (byte)(xl >> 16);
        data[i + 2] = (byte)(xl >> 8);
        data[i + 3] = (byte)(xl);
        data[i + 4] = (byte)(xr >> 24);
        data[i + 5] = (byte)(xr >> 16);
        data[i + 6] = (byte)(xr >> 8);
        data[i + 7] = (byte)(xr);
    }
}

我的加密方法:

private void EncryptFile(string szFilePath, string szInfoFile, string szKey, string szEncryptedFile = "")
        {
            // Blowfish
            Blowfish alg = new Blowfish(Encoding.Unicode.GetBytes(szKey));

            // Open file
            System.IO.FileStream originalStream = System.IO.File.OpenRead(szFilePath);

            // Store original file length
            long originalLength = originalStream.Length;
            System.IO.File.WriteAllText(szInfoFile, originalLength.ToString());

            Byte[] buffer = new byte[originalStream.Length + (originalStream.Length % 8)];
            originalStream.Read(buffer, 0, buffer.Length);
            originalStream.Close();

            // Encrypt
            alg.Encipher(buffer, buffer.Length);

            string szEncFile;

            if (szEncryptedFile != string.Empty) szEncFile = szEncryptedFile; else szEncFile = szFilePath;

            System.IO.FileStream stream = new System.IO.FileStream(szEncFile, System.IO.FileMode.Create);
            stream.Write(buffer, 0, buffer.Length);
            stream.Close(); 
        }

感谢。

2 个答案:

答案 0 :(得分:3)

如果你要做的是将值舍入到可被8整除的下一个值,那么你应该这样做:

Byte[] buffer = new byte[originalStream.Length + (8-(originalStream.Length % 8))];

答案 1 :(得分:2)

Peter Ritchie answered它。但是,下面应该考虑一些惯用的部分。一种是在IDisposable块中包装FileStream - 实现的类(例如using s),以确保在处理期间出现异常情况时处理资源。另一个是你放在一行的if..then。这真的很奇怪。我用三元运算符取而代之,这似乎符合你所使用的用法。祝你好运。

private void EncryptFile(string szFilePath, string szInfoFile, string szKey, string szEncryptedFile = "")
    {
        // Blowfish
        Blowfish alg = new Blowfish(Encoding.Unicode.GetBytes(szKey));

        // Open file
        using (System.IO.FileStream originalStream = System.IO.File.OpenRead(szFilePath))
        {
            // Store original file length
            long originalLength = originalStream.Length;
            System.IO.File.WriteAllText(szInfoFile, originalLength.ToString());

            Byte[] buffer = new byte[originalStream.Length + (originalStream.Length % 8)];
            originalStream.Read(buffer, 0, buffer.Length);
        }

        // Encrypt
        alg.Encipher(buffer, buffer.Length);

        string szEncFile;

        szEncFile = string.IsNullOrEmpty(szEncryptedFile) ? szFilePath : szEncryptedFile;

        using (System.IO.FileStream stream = new System.IO.FileStream(szEncFile, System.IO.FileMode.Create))
        {
            stream.Write(buffer, 0, buffer.Length);
        }
    }