c# - 如何在字节通过流时编辑字节?

标签 c# replace stream byte edit

我的目标是让一个文件流打开一个用户选择的文件,然后,它应该以大约 4mb 的 block (缓冲区)的形式流式传输文件字节(这可以更改,只是为了好玩)。当字节(以 block 的形式)通过流时,我想要一个循环 if 语句来查看字节值是否包含在我在别处声明的数组中。 (下面的代码将构建一个用于替换字节的随机数组),替换循环可以像底部的 for 循环一样。如您所见,我对这种语言相当流利,但由于某种原因,当它们从一个文件读取到一个新文件时,我无法编辑和重写这些 block 。提前致谢!

    private void button2_Click(object sender, EventArgs e)
    {
        GenNewKey();

        const int chunkSize = 4096; // read the file by chunks of 4KB
        using (var file = File.OpenRead(textBox1.Text))
        {
            int bytesRead;
            var buffer = new byte[chunkSize];
            while ((bytesRead = file.Read(buffer, 0, buffer.Length)) > 0)
            {
                byte[] newbytes = buffer;
                int index = 0;
                foreach (byte b in buffer)
                {
                    for (int x = 0; x < 256; x++)
                    {
                        if (buffer[index] == Convert.ToByte(lst[x]))
                        {
                            try
                            {
                                newbytes[index] = Convert.ToByte(lst[256 - x]);
                            }
                            catch (System.Exception ex)
                            {
                                //just to show why the error was thrown, but not really helpful..
                                MessageBox.Show(index + ", " + newbytes.Count().ToString());
                            }
                        }
                    }
                    index++;
                }
                AppendAllBytes(textBox1.Text + ".ENC", newbytes);
            }
        }
    }

    private void GenNewKey()
    {
        Random rnd = new Random();

        while (lst.Count < 256)
        {
            int x = rnd.Next(0, 255);
            if (!lst.Contains(x))
            {
                lst.Add(x);
            }
        }

        foreach (int x in lst)
        {
            textBox2.Text += ", " + x.ToString();
            //just for me to see what was generated
        }
    }

    public static void AppendAllBytes(string path, byte[] bytes)
    {
        if (!File.Exists(path + ".ENC"))
        {
            File.Create(path + ".ENC");
        }
        using (var stream = new FileStream(path, FileMode.Append))
        {
            stream.Write(bytes, 0, bytes.Length);
        }
    }

其中 textbox1 保存要加密的文件的路径和名称,textBox2 保存生成的密码用于个人调试目的,按钮二是加密按钮,当然我使用的是 System.IO。

最佳答案

事实上,您在 newbytes[index] = Convert.ToByte(lst[256 - x]) 中有一个错误关闭

如果 x 为 0,则您将拥有 lst[256],但是 lst 仅在 0-255 之间。将其更改为 255 应该可以解决问题。

它卡住的原因是你的程序效率极低并且在 UI 线程上工作(还有一些错误,比如你在处理 buffer 时应该只达到 bytesRead 大小,但这只会在输出中为您提供不应该存在的额外数据。此外,您正在为 buffernewbytes 重用相同的数组,因此您的内部for 循环可以多次修改同一个索引,因为每次您执行 newbytes[index] = Convert.ToByte(lst[256 - x]) 您都在修改 buffer[index] 将在 for 循环的下一次迭代中再次检查)。

有很多方法可以改进你的代码,这里有一个片段与你正在做的类似(我不做整个“找到索引并使用相反的位置”,我只是使用作为数组中的索引传入的字节)。

while ((bytesRead = file.Read(buffer, 0, buffer.Length)) > 0)
{
    byte[] newbytes = new byte[bytesRead];
    for(int i = 0; i < newbytes.Length; i++)
    {
        newbytes[i] = (byte)lst[buffer[i]]))
    }
    AppendAllBytes(textBox1.Text + ".ENC", newbytes);
}

这也可能导致卡住,但不会那么严重,要解决释放问题,您应该将所有这些代码放入 BackgroundWorker 或类似的程序中,以便在另一个线程上运行。

关于c# - 如何在字节通过流时编辑字节?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20639566/

相关文章:

c# - C 源代码到 DLL 以便在 C# 应用程序中使用

c# - 将字符串数组转换为 C# 中的串联字符串

ruby - Ruby 中 gsub 的奇怪行为

c# - MSBuild构建的程序集无法注册,RegAsm(错误RA000);与 Visual Studio 2010 完全相同的 .csProj 创建有效的程序集?

c# - 是否可以读取.exe文件?

r - 在 R 中,如何用等于相同宽度的值向量替换多列中的值?

sed - 查找并替换为相同的通配符

.net - 如何阻止 XPathDocument 关闭流?

iphone - ios流mp3并录制声音

python - tweepy stream.filter() 方法不能正常工作