C#:文件RSA加密解密问题 十万火急!!!

lanbo1230 2006-06-06 08:35:47
我从网上下了一个RSA文件加密解密类,实现后
加密时没问题,解密时老提示我:
未处理的“System.Security.Cryptography.CryptographicException”类型的异常出现在 mscorlib.dll 中。
其他信息: 不正确的数据。
望高手给予解决

下面是相关代码
解密代码:
public void Decrypt(string inFileName, string outFileName, string PrivateKey)
{
rsa.FromXmlString(PrivateKey);
FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);//输
FileStream fout = new FileStream(outFileName,OpenOrCreate, FileAccess.Write);//
fout.SetLength(0);//将该流的长度设置为给定值。如果给定值小于流
byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fin.Length;//获取用字节表示的流长度。
int len;
while (rdlen < totlen)
{
byte[] bout;
len = fin.Read(bin, 0, 100);//从输入流中读取字节块并将该数据写入给定缓
bout = rsa.Decrypt(bin, false);///***执行到这一行就提示上述的错误***
fout.Write(bout, 0, bout.Length);
rdlen = rdlen + len;
}
fout.Close();
fin.Close();
}

加密代码
public void Encrypt(string inFileName, string outFileName, string PublicKey)
{
rsa.FromXmlString(PublicKey);
FileStream fin = new FileStream(inFileName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(outFileName, de.OpenOrCreate,FileAccess.Write);
fout.SetLength(0);
byte[] bin = new byte[100];
long rdlen = 0;
long totlen = fin.Length;
int len;
while (rdlen < totlen)
{
byte[] bout;
len = fin.Read(bin, 0, 100);//从输入流中读取数据到字节数组bin中的0~100
bout = rsa.Encrypt(bin,false);//对bin这部分数据加密,并把加密后的字节
fout.Write(bout, 0, bout.Length);//把bout长度的数据写到输出流中;
rdlen = rdlen + len;
}

fout.Close();
fin.Close();
}

完整的类在下面地址里:
http://www.2182.cn/bbs/Show.Asp?ID=244
...全文
254 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lanbo1230 2006-06-06
  • 打赏
  • 举报
回复
就没高手 来看看???
lanbo1230 2006-06-06
  • 打赏
  • 举报
回复
大哥,我要做的毕业设计啊,不整点东西怎么行.
还有个VC.NET连数据库的问题,
这方面你怎么样啊?
dyc13 2006-06-06
  • 打赏
  • 举报
回复
真不明白,为什么谈到加密解密,总是有人提起md5、RSA之类的东西了。。
自已随便写个不就得了?

110,499

社区成员

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

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

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