求大神帮帮忙啊,帮我看看这样写那有问题,c#简单加密问题

北漂90后大叔 2015-10-20 05:39:56
老手让把文件转换成byst[],并把其中的数+1,简单的加密,使文件让其他人打不开,然后再把刚刚的过程反过来,使byst[]中的数-1,使文件解密,我写了下面的代码,但是只能执行一次+1或-1,不能再反过来解密刚毕业,啥也不会,求解救!!!咋解决,跪求啊

private void button3_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
byte[] infbytes = new byte[fs.Length];
fs.Read(infbytes, 0, infbytes.Length);
fs.Close();
byte[] bytes={};
int[] inn = { };

for (int i = 0; i == infbytes.Length; i++)
{
inn[i] = infbytes[i]+1;
bytes[i] = (byte)inn[i];
}

File.WriteAllBytes(textBox1.Text, bytes);
MessageBox.Show("加密成功");
}

private void button4_Click(object sender, EventArgs e)
{
FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
byte[] infbytes = new byte[fs.Length];
fs.Read(infbytes, 0, infbytes.Length);
fs.Close();
byte[] bytes = {};
int[] inn = { };

for (int i = 0; i == infbytes.Length; i++)
{
inn[i] = infbytes[i] - 1;
bytes[i] = (byte)inn[i];
}

File.WriteAllBytes(textBox1.Text, bytes);
MessageBox.Show("解密成功");
}

错误如下:
...全文
83 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
北漂90后大叔 2015-10-20
  • 打赏
  • 举报
回复
引用 7 楼 ajianchina 的回复:
搞定了别忘记结贴

private void button3_Click(object sender, EventArgs e)
{
	FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
	BinaryReader r = new BinaryReader(fs);
	r.BaseStream.Seek(0, SeekOrigin.Begin);
	byte[] bytes = r.ReadBytes((int)r.BaseStream.Length);
	for(int i = 0;i<bytes.Length;i++)
	{
		bytes[i] += 1;
	}
	File.WriteAllBytes(textBox1.Text, bytes);
	MessageBox.Show("加密成功");
}

private void button4_Click(object sender, EventArgs e)
{
	FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
	BinaryReader r = new BinaryReader(fs);
	r.BaseStream.Seek(0, SeekOrigin.Begin);
	byte[] bytes = r.ReadBytes((int)r.BaseStream.Length);
	for (int i = 0; i < bytes.Length; i++)
	{
		bytes[i] -= 1;
	}
	File.WriteAllBytes(textBox1.Text, bytes);
	MessageBox.Show("解密成功");
}
谢谢大哥,必须结贴,终于下班了!
ajianchina 2015-10-20
  • 打赏
  • 举报
回复
没测,上面的代码应该会有异常,加一下释放

private void button3_Click(object sender, EventArgs e)
{
	byte[] bytes;
	using (FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read))
	{
		BinaryReader r = new BinaryReader(fs);
		r.BaseStream.Seek(0, SeekOrigin.Begin);
		bytes = r.ReadBytes((int)r.BaseStream.Length);
		for (int i = 0; i < bytes.Length; i++)
		{
			bytes[i] += 1;
		}
	   
	}
	File.WriteAllBytes(textBox1.Text, bytes);
	MessageBox.Show("加密成功");
}




private void button4_Click(object sender, EventArgs e)
{
	byte[] bytes;
	using (FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read))
	{
		BinaryReader r = new BinaryReader(fs);
		r.BaseStream.Seek(0, SeekOrigin.Begin);
		bytes = r.ReadBytes((int)r.BaseStream.Length);
		for (int i = 0; i < bytes.Length; i++)
		{
			bytes[i] -= 1;
		}
	}
	File.WriteAllBytes(textBox1.Text, bytes);
	MessageBox.Show("解密成功");
}
ajianchina 2015-10-20
  • 打赏
  • 举报
回复
搞定了别忘记结贴

private void button3_Click(object sender, EventArgs e)
{
	FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
	BinaryReader r = new BinaryReader(fs);
	r.BaseStream.Seek(0, SeekOrigin.Begin);
	byte[] bytes = r.ReadBytes((int)r.BaseStream.Length);
	for(int i = 0;i<bytes.Length;i++)
	{
		bytes[i] += 1;
	}
	File.WriteAllBytes(textBox1.Text, bytes);
	MessageBox.Show("加密成功");
}

private void button4_Click(object sender, EventArgs e)
{
	FileStream fs = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
	BinaryReader r = new BinaryReader(fs);
	r.BaseStream.Seek(0, SeekOrigin.Begin);
	byte[] bytes = r.ReadBytes((int)r.BaseStream.Length);
	for (int i = 0; i < bytes.Length; i++)
	{
		bytes[i] -= 1;
	}
	File.WriteAllBytes(textBox1.Text, bytes);
	MessageBox.Show("解密成功");
}
北漂90后大叔 2015-10-20
  • 打赏
  • 举报
回复
引用 1 楼 wg5945 的回复:
i < infbytes.Length
写成小于也不行啊!!!大神求解救啊!!!
北漂90后大叔 2015-10-20
  • 打赏
  • 举报
回复
各位,大神,快!求解救啊,别老笑了[/img][/img]
crystal_lz 2015-10-20
  • 打赏
  • 举报
回复
引用 3 楼 wyd1520 的回复:
byte[] bytes=new byte[infbytes.Length]; int[] inn = new byte[infbytes.Length]; for (int i = 0; i <= infbytes.Length; i++) { inn[i] = infbytes[i]+1; bytes[i] = (byte)inn[i]; } byte[] bytes=new byte[infbytes.Length]; int[] inn = new byte[infbytes.Length]; for (int i = 0; i <= infbytes.Length; i++) { inn[i] = infbytes[i]-1; bytes[i] = (byte)inn[i]; }
你和他是同一个老师吗? <=
本拉灯 2015-10-20
  • 打赏
  • 举报
回复
byte[] bytes=new byte[infbytes.Length]; int[] inn = new byte[infbytes.Length]; for (int i = 0; i <= infbytes.Length; i++) { inn[i] = infbytes[i]+1; bytes[i] = (byte)inn[i]; } byte[] bytes=new byte[infbytes.Length]; int[] inn = new byte[infbytes.Length]; for (int i = 0; i <= infbytes.Length; i++) { inn[i] = infbytes[i]-1; bytes[i] = (byte)inn[i]; }
Poopaye 2015-10-20
  • 打赏
  • 举报
回复
我很好奇是谁教你这样写for循环的?可以揍他。
wg5945 2015-10-20
  • 打赏
  • 举报
回复
i < infbytes.Length

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧