因为RSA不能解密大于128字符的密文,那这个密码用途不就局限了吗?还有写了一个RSA的程序如何测试呢,一般加密出来的密文都大于128字符啊?
代码如下:
产生密钥对:
private void button3_Click(object sender, System.EventArgs e)
{
string pubkey = rsa.ToXmlString(false);
string prikey = rsa.ToXmlString(true);
FileStream fsFile = new FileStream( "jiami.xml ",FileMode.Create);
StreamWriter fsWrite=new StreamWriter(fsFile);
fsWrite.WriteLine(prikey);
FileStream fsFile1 = new FileStream( "jiemi.xml ",FileMode.Create);
StreamWriter fsWrite1=new StreamWriter(fsFile1);
fsWrite1.WriteLine(pubkey);
fsWrite.Close();
fsFile.Close();
fsWrite1.Close();
fsFile1.Close();
}
加密:
StreamReader reader=new StreamReader( "jiami.xml ");
string sKey=reader.ReadToEnd();
reader.Close();
FileStream fsFile = new FileStream( "aa.txt ",FileMode.Open);
StreamReader fsRead=new StreamReader(fsFile,System.Text.Encoding.GetEncoding( "GB2312 "),true);
FileStream fsFile1 = new FileStream( "bb.txt ",FileMode.Create);
StreamWriter fsWrite=new StreamWriter(fsFile1);
string sStr=fsRead.ReadLine();
while(sStr != null)
{
rsa.FromXmlString(sKey);
byte[] zz=Encoding.GetEncoding( "gb2312 " ).GetBytes(sStr);
byte[] done1 = rsa.Encrypt(zz,false);
fsWrite.WriteLine(Convert.ToBase64String(done1));
sStr=fsRead.ReadLine();
}
fsRead.Close();
fsFile.Close();
fsWrite.Close();
fsFile1.Close();
解密:
private void button2_Click(object sender, System.EventArgs e)
{
StreamReader reader=new StreamReader( "jiemi.xml ");
string sKey=reader.ReadToEnd();
reader.Close();
FileStream fsFile = new FileStream( "bb.txt ",FileMode.Open);
StreamReader fsRead=new StreamReader(fsFile,System.Text.Encoding.GetEncoding( "GB2312 "),true);
FileStream fsFile1 = new FileStream( "cc.txt ",FileMode.Create);
StreamWriter fsWrite=new StreamWriter(fsFile1);
string sStr=fsRead.ReadLine();
while(sStr != null)
{
rsa.FromXmlString(sKey);
byte[] done1 = rsa.Decrypt(Convert.FromBase64String(sStr),false);
fsWrite.WriteLine(Encoding.GetEncoding( "gb2312 " ).GetString(done1));
sStr=fsRead.ReadLine();
}
fsRead.Close();
fsFile.Close();
fsWrite.Close();
fsFile1.Close();
}
当程序运行时,我加密没问题,解密的时候就提示:不正确的项 ,密文不能大于128字符啊
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!