老手让把文件转换成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("解密成功");
}
错误如下:
------解决思路----------------------
i < infbytes.Length
------解决思路----------------------
我很好奇是谁教你这样写for循环的?可以揍他。
------解决思路----------------------
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];
}
------解决思路----------------------
你和他是同一个老师吗? <=
------解决思路----------------------
搞定了别忘记结贴
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("解密成功");
}
------解决思路----------------------
没测,上面的代码应该会有异常,加一下释放
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("解密成功");
}