我运行环境是windows2000 .net1.1 
using System.Text;
using System.Security.Cryptography; 
using System.Security;
执行代码如下:        
RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
rsa.FromXmlString("990000");
byte[] test=System.Text.Encoding.ASCII.GetBytes("测试Rsa签名");
         rsa.Decrypt(test,false);           //此行报的错误信息
错误信息是:{"行 1 - 意外的文件尾。 中有无效语法。" } 
我做的目的就是要对一个byte[] 用Rsa签名,要加一个私钥("990000")
怎么实现不了.请高手指点一下.在这里表示万分感谢

解决方案 »

  1.   

    using System;
    using System.IO;
    using System.Text;
    using System.Security.Cryptography;/// <summary>
    /// 一个简单的使用.NET非对称加密算法的例子
    /// 本例的程序很简单,仅用于说明如何在.NET里面使用非对称(RSA)算法。
    /// Kwanhong 2005.9
    /// </summary>
    class Class1
    {
     public static void Main(string[] args)
     {
      Class1 c=new Class1();
      c.StartDemo();
     } public void StartDemo()
     {
      //RSA的加解密过程:
      //  有 rsa1 和 rsa2 两个RSA对象。
      //  现在要 rsa2 发送一段信息给 rsa1, 则先由 rsa1 发送“公钥”给 rsa2
      //  rsa2 获取得公钥之后,用来加密要发送的数据内容。
      //  rsa1 获取加密后的内容后,用自己的私钥解密,得出原始的数据内容。  RSACryptoServiceProvider rsa1 = new RSACryptoServiceProvider();
      RSACryptoServiceProvider rsa2 = new RSACryptoServiceProvider();  string publickey;
      publickey=rsa1.ToXmlString(false);  //导出 rsa1 的公钥  string plaintext;
      plaintext="你好吗?这是用于测试的字符串。";  //原始数据
      Console.WriteLine("原始数据是:\n{0}\n",plaintext);  rsa2.FromXmlString(publickey); //rsa2 导入 rsa1 的公钥,用于加密信息  //rsa2开始加密
      byte[] cipherbytes;
      cipherbytes=rsa2.Encrypt(
       Encoding.UTF8.GetBytes(plaintext),
       false);  /*//////////////////////////////////////////////*/
      Console.WriteLine("加密后的数据是:");
      for(int i=0; i< cipherbytes.Length; i++)
      {
       Console.Write("{0:X2} ",cipherbytes[i]);
      }
      Console.WriteLine("\n");
      /*//////////////////////////////////////////////*/  //rsa1开始解密
      byte[] plaintbytes;
      plaintbytes = rsa1.Decrypt(cipherbytes,false);  Console.WriteLine("解密后的数据是:");
      Console.WriteLine(Encoding.UTF8.GetString(plaintbytes));  Console.ReadLine();
     }}
      

  2.   

    jxufewbt 没有要需求回答啊,你是用.net本身带的,我要实现的是自定义值的公钥加密
      rsa.Decrypt(test,false);           //此行报的错误信息
    错误信息是:{"行 1 - 意外的文件尾。 中有无效语法。" } 
    在加密时出错,不知道原因,主要的就是自定义的公钥添加不上,示便中我添加的程序还报错.不知道如何解决
      

  3.   

    上面的哥哥再问一句
    rsa2.FromXmlString(publickey); //rsa2 导入 rsa1 的公钥,用于加密信息
    与rsa2.FromXmlString("99000");
    两个返回值的是否是一样一样呢.问题会不会出现在这里.要不然不会报什么文件结尾错误啊