首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Blowfish无效长度异常

Blowfish无效长度异常
EN

Stack Overflow用户
提问于 2012-08-27 22:22:17
回答 2查看 323关注 0票数 1

我正在用Blowfish算法加密我的文件,但我似乎对此一无所知。每当我尝试Encipher()时,它都会抛出一个异常,显示'Invalid Length‘。我计算出长度必须是0,当它被修改为8的时候,我认为这意味着应该有8乘8的流块来开始加密。我该怎么办?

Blowfish的加密方法:

代码语言:javascript
复制
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);
    }
}

我的加密方法:

代码语言:javascript
复制
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(); 
        }

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-27 22:44:44

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

代码语言:javascript
复制
Byte[] buffer = new byte[originalStream.Length + (8-(originalStream.Length % 8))];
票数 3
EN

Stack Overflow用户

发布于 2012-08-27 22:55:17

Peter Ritchie answered it。然而,下面有几个你应该考虑的习惯用法。一种是将IDisposable-implemented类(如FileStream)包装在using块中,以确保在处理过程中出现异常情况时处置资源。另一个是您放在一行中的if..then。这真的..。很奇怪。我已经将其替换为三元运算符,这似乎适合您使用的用法。祝你好运。

代码语言:javascript
复制
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);
        }
    }
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12143873

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档