关于文件用对称加密后,无法解密的问题! 1,在加密时应该清空test1.txt,然后再写其他的我试一下 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 to: gujianxin(木头象) 我已清空了!test1.txt,就是一个新建的文件test1.txt都没有动过 在最后一段中,你用RijndaelManaged symm = new RijndaelManaged();加密和解密用的不是同一个symm! 试一试定义全局变量symm(我没有用过RijndaelManaged,只用过rsa 和des。) to:12wywy() 还是不行,同样的错误!!再说,如果必须用同一个symm,那我加密的文件,要同时解密才可以??实际上是,文件加密后,过段时间再解密,不可能用同一个symm啊? 需要使用相同的RijndaelManaged对象,如果要在不同场合使用,请使用 Key 和初始化向量 (IV) 来生成private RijndaelManaged symm = new RijndaelManaged();//全局public void test(){ FileStream fsIn = File.Open(@"C:\test.txt",FileMode.Open,FileAccess.Read); FileStream fsOut=File.Open(@"C:\test1.txt",FileMode.Open,FileAccess.Write); fsOut.SetLength(0); //清空//使用全局 RijndaelManaged symm = new RijndaelManaged(); ICryptoTransform transform = symm.CreateEncryptor(); CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write); BinaryReader br = new BinaryReader(fsIn); cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length); cstream.FlushFinalBlock(); cstream.Close(); fsIn.Close(); fsOut.Close();} to:gujianxin(木头象) 你这种方法我试了!如下:用iv,key的也试过,但也出现同样的错误!不知道为什么,郁闷中 我的代码如下:public class WebForm1 : System.Web.UI.Page{ protected System.Web.UI.WebControls.TextBox TextBox1; protected System.Web.UI.WebControls.TextBox TextBox2; protected System.Web.UI.WebControls.Button Button1; protected System.Web.UI.WebControls.TextBox TextBox3; protected System.Web.UI.WebControls.Button Button2; private RijndaelManaged symm = new RijndaelManaged(); private void Button1_Click(object sender, System.EventArgs e) { FileStream fsOut = File.Open(@"C:\\test1.txt",FileMode.Open,FileAccess.Read); ICryptoTransform transform = symm.CreateDecryptor(); CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Read); StreamReader sr = new StreamReader(cstream,System.Text.Encoding.GetEncoding("GB2312")); TextBox3.Text = sr.ReadToEnd();//出错的地方!!!! fsOut.Close(); } private void Button2_Click(object sender, System.EventArgs e) { FileStream fsIn = File.Open(@"C:\test.txt" , FileMode.Open , FileAccess.Read); FileStream fsOut = File.Open(@"C:\test1.txt" , FileMode.Open , FileAccess.Write); ICryptoTransform transform = symm.CreateEncryptor(); CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write); BinaryReader br = new BinaryReader(fsIn); cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length); cstream.FlushFinalBlock(); cstream.Close(); fsIn.Close(); fsOut.Close(); }} CryptoStream 是从流到流的加密,你想获得加密后的内容,可以先保存了再取public string test(){ FileStream fsIn = File.Open(@"C:\test.txt",FileMode.Open,FileAccess.Read); FileStream fsOut=File.Open(@"C:\test1.txt",FileMode.Open,FileAccess.Write|FileAccess.Read ); fsOut.SetLength(0); //清空//使用全局 RijndaelManaged symm = new RijndaelManaged(); ICryptoTransform transform = symm.CreateEncryptor(); CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write); BinaryReader br = new BinaryReader(fsIn); cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length); cstream.FlushFinalBlock(); cstream.Close(); fsIn.Close(); fsOut.Close(); StreamReader sr = new StreamReader(@"c:\test1.txt",System.Text.Encoding.GetEncoding("GB2312")); return sr.ReadToEnd();//现在不会错了!!!} private void button1_Click(object sender, System.EventArgs e){ Password p = new Password(); this.textBox1.Text = p.test();}private void button2_Click(object sender, System.EventArgs e){ Password p = new Password(); this.textBox1.Text = p.test2();}public class Password{ public string test() { FileStream fsIn = File.Open(@"C:\test.txt",FileMode.Open,FileAccess.Read); FileStream fsOut=File.Open(@"C:\test1.txt",FileMode.Open,FileAccess.Write|FileAccess.Read ); fsOut.SetLength(0); //清空 //使用全局 RijndaelManaged symm = new RijndaelManaged(); ICryptoTransform transform = symm.CreateEncryptor(); CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write); BinaryReader br = new BinaryReader(fsIn); cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length); cstream.FlushFinalBlock(); cstream.Close(); fsIn.Close(); fsOut.Close(); StreamReader sr = new StreamReader(@"c:\test1.txt",System.Text.Encoding.GetEncoding("GB2312")); string s= sr.ReadToEnd();//出错的地方!!!! sr.Close(); return s; } public string test2() { FileStream fsIn = File.Open(@"C:\test1.txt",FileMode.Open,FileAccess.Read); FileStream fsOut=File.Open(@"C:\test2.txt",FileMode.Open ,FileAccess.Write|FileAccess.Read ); fsOut.SetLength(0); //清空 //使用全局 RijndaelManaged symm = new RijndaelManaged(); ICryptoTransform transform = symm.CreateDecryptor(); CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write); BinaryReader br = new BinaryReader(fsIn); cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length); cstream.FlushFinalBlock(); cstream.Close(); fsIn.Close(); fsOut.Close(); StreamReader sr = new StreamReader(@"c:\test2.txt",System.Text.Encoding.GetEncoding("GB2312")); string s= sr.ReadToEnd();//出错的地方!!!! sr.Close(); return s; }} http://lucky_elove.www1.dotnetplayground.com/ShowDetail.aspx?id=7AE7D20A-A5DA-4303-AC2D-32046BE4D086 可以了!给分!!!但你能再帮我解决一下这个疑问吗?如果必须用同一个symm,那我加密的文件,要同时解密才可以??实际上是,文件加密后,过段时间再解密,不可能用同一个symm啊?如果用key值,怎么的流程??是否我要先要保存一个KEY,每个文件都用这个KEY来解密??? c# 安装部署后的安装包 按钮的拖曳 c#网络+数据库编程,顺便请教几个问题 超高分的超简单问题:DataGridView合并单元格的问题, 如何避免拖动滚动条时显示错乱? 怎么得知SQL语句搜索到结果~~~~???? vs2008中,双引号问题 求教:进销存业务提成问题 关于.NET制作安装程序的问题! 水晶报表的奇怪问题 对c#编写的web.config配置文件修改,需不需要重启IIS服务 一个关于自定义打印纸张的问题 有谁知道如何用C#给上传的图片加上水印文字?我知道怎么生成缩略图
我已清空了!test1.txt,就是一个新建的文件test1.txt都没有动过
RijndaelManaged symm = new RijndaelManaged();加密和解密用的不是同一个symm! 试一试定义全局变量symm(我没有用过RijndaelManaged,只用过rsa 和des。)
还是不行,同样的错误!!再说,如果必须用同一个symm,那我加密的文件,要同时解密才可以??实际上是,文件加密后,过段时间再解密,不可能用同一个symm啊?
private RijndaelManaged symm = new RijndaelManaged();//全局public void test()
{
FileStream fsIn = File.Open(@"C:\test.txt",FileMode.Open,FileAccess.Read);
FileStream fsOut=File.Open(@"C:\test1.txt",FileMode.Open,FileAccess.Write);
fsOut.SetLength(0); //清空
//使用全局 RijndaelManaged symm = new RijndaelManaged();
ICryptoTransform transform = symm.CreateEncryptor();
CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write); BinaryReader br = new BinaryReader(fsIn); cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length);
cstream.FlushFinalBlock();
cstream.Close();
fsIn.Close();
fsOut.Close();}
你这种方法我试了!如下:用iv,key的也试过,但也出现同样的错误!
不知道为什么,郁闷中
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Button Button2;
private RijndaelManaged symm = new RijndaelManaged(); private void Button1_Click(object sender, System.EventArgs e)
{ FileStream fsOut = File.Open(@"C:\\test1.txt",FileMode.Open,FileAccess.Read);
ICryptoTransform transform = symm.CreateDecryptor();
CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Read);
StreamReader sr = new StreamReader(cstream,System.Text.Encoding.GetEncoding("GB2312"));
TextBox3.Text = sr.ReadToEnd();//出错的地方!!!!
fsOut.Close();
} private void Button2_Click(object sender, System.EventArgs e)
{ FileStream fsIn = File.Open(@"C:\test.txt" , FileMode.Open , FileAccess.Read);
FileStream fsOut = File.Open(@"C:\test1.txt" , FileMode.Open , FileAccess.Write);
ICryptoTransform transform = symm.CreateEncryptor();
CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write);
BinaryReader br = new BinaryReader(fsIn); cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length);
cstream.FlushFinalBlock();
cstream.Close();
fsIn.Close();
fsOut.Close();
}}
public string test()
{
FileStream fsIn = File.Open(@"C:\test.txt",FileMode.Open,FileAccess.Read);
FileStream fsOut=File.Open(@"C:\test1.txt",FileMode.Open,FileAccess.Write|FileAccess.Read );
fsOut.SetLength(0); //清空
//使用全局 RijndaelManaged symm = new RijndaelManaged();
ICryptoTransform transform = symm.CreateEncryptor();
CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write); BinaryReader br = new BinaryReader(fsIn); cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length);
cstream.FlushFinalBlock();
cstream.Close();
fsIn.Close();
fsOut.Close(); StreamReader sr = new StreamReader(@"c:\test1.txt",System.Text.Encoding.GetEncoding("GB2312"));
return sr.ReadToEnd();//现在不会错了!!!}
{
Password p = new Password();
this.textBox1.Text = p.test();}private void button2_Click(object sender, System.EventArgs e)
{
Password p = new Password();
this.textBox1.Text = p.test2();
}public class Password
{
public string test()
{
FileStream fsIn = File.Open(@"C:\test.txt",FileMode.Open,FileAccess.Read);
FileStream fsOut=File.Open(@"C:\test1.txt",FileMode.Open,FileAccess.Write|FileAccess.Read );
fsOut.SetLength(0); //清空
//使用全局 RijndaelManaged symm = new RijndaelManaged();
ICryptoTransform transform = symm.CreateEncryptor();
CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write); BinaryReader br = new BinaryReader(fsIn); cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length);
cstream.FlushFinalBlock();
cstream.Close();
fsIn.Close();
fsOut.Close(); StreamReader sr = new StreamReader(@"c:\test1.txt",System.Text.Encoding.GetEncoding("GB2312"));
string s= sr.ReadToEnd();//出错的地方!!!!
sr.Close();
return s;
} public string test2()
{
FileStream fsIn = File.Open(@"C:\test1.txt",FileMode.Open,FileAccess.Read);
FileStream fsOut=File.Open(@"C:\test2.txt",FileMode.Open ,FileAccess.Write|FileAccess.Read );
fsOut.SetLength(0); //清空
//使用全局 RijndaelManaged symm = new RijndaelManaged();
ICryptoTransform transform = symm.CreateDecryptor();
CryptoStream cstream = new CryptoStream(fsOut,transform,CryptoStreamMode.Write);
BinaryReader br = new BinaryReader(fsIn);
cstream.Write(br.ReadBytes((int)fsIn.Length),0,(int)fsIn.Length);
cstream.FlushFinalBlock();
cstream.Close();
fsIn.Close();
fsOut.Close();
StreamReader sr = new StreamReader(@"c:\test2.txt",System.Text.Encoding.GetEncoding("GB2312"));
string s= sr.ReadToEnd();//出错的地方!!!!
sr.Close();
return s;
}
}
但你能再帮我解决一下这个疑问吗?如果必须用同一个symm,那我加密的文件,要同时解密才可以??
实际上是,文件加密后,过段时间再解密,不可能用同一个symm啊?
如果用key值,怎么的流程??
是否我要先要保存一个KEY,每个文件都用这个KEY来解密???