vb 的可以吗!!!我现在没有时间把她改写为c# 的

解决方案 »

  1.   

    你可以看看
    System.Security.Cryptography.RSA 类
      

  2.   

    不对称算法通常用于加密少量数据,如加密对称密钥和 IV。通常,执行不对称加密的个人使用由另一方生成的公钥。.NET Framework 为此目的而提供了 RSACryptoServiceProvider 类。
    下面的示例使用公钥信息加密对称公钥和 IV。初始化表示第三方公钥的两个字节数组。RSAParameters 对象被初始化为这些值。接着,使用 RSACryptoServiceProvider.ImportParameters 方法将 RSAParameters(及其所表示的公钥)导入到 RSACryptoServiceProvider 中。最后,加密 RijndaelManaged 类所创建的私钥和 IV。本示例要求系统安装有 128 位加密。using System;
    using System.Security.Cryptography;class Class1
    {
       static void Main()
       {
          //initialze the byte arrays to the public key information.
          byte[] PublicKey = {214,46,220,83,160,73,40,39,201,155,19,202,3,11,191,178,56,
                74,90,36,248,103,18,144,170,163,145,87,54,61,34,220,222,
                207,137,149,173,14,92,120,206,222,158,28,40,24,30,16,175,
                108,128,35,230,118,40,121,113,125,216,130,11,24,90,48,194,
                240,105,44,76,34,57,249,228,125,80,38,9,136,29,117,207,139,
                168,181,85,137,126,10,126,242,120,247,121,8,100,12,201,171,
                38,226,193,180,190,117,177,87,143,242,213,11,44,180,113,93,
                106,99,179,68,175,211,164,116,64,148,226,254,172,147};      byte[] Exponent = {1,0,1};
          
          //Values to store encrypted symmetric keys.
          byte[] EncryptedSymmetricKey;
          byte[] EncryptedSymmetricIV;      //Create a new instance of RSACryptoServiceProvider.
          RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();      //Create a new instance of RSAParameters.
          RSAParameters RSAKeyInfo = new RSAParameters();      //Set RSAKeyInfo to the public key values. 
          RSAKeyInfo.Modulus = PublicKey;
          RSAKeyInfo.Exponent = Exponent;      //Import key parameters into RSA.
          RSA.ImportParameters(RSAKeyInfo);      //Create a new instance of the RijndaelManaged class.
          RijndaelManaged RM = new RijndaelManaged();      //Encrypt the symmetric key and IV.
          EncryptedSymmetricKey = RSA.Encrypt(RM.Key, false);
          EncryptedSymmetricIV = RSA.Encrypt(RM.IV, false);
       }
    }
      

  3.   

    不对称解密
    生成公钥/私钥对的用户通常执行不对称解密。可以使用用于创建公钥/私钥对的 RSACryptoServiceProvider 来解密数据或者用密钥信息初始化一个新的 RSACryptoServiceProvider。只有使用与用于加密数据的公钥相对应的私钥,解密才能成功。
    下面的示例阐释如何对表示一个对称密钥和 IV 的两个字节数组进行解密。//Create a new instance of RSACryptoServiceProvider.
    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();//Decrypt the symetric key and IV.
    SymetricKey = RSA.Decrypt( EncryptedSymetricKey, false);
    SymetricIV = RSA.Decrypt( EncryptedSymetricIV , false);
      

  4.   

    你比较星云,我找到了,以前的代码 !!自己看看吧 !!!但是要说明一点!!该方式只能加密长度有限的字符!!!
    大该是 58 个字符//####################################################################
    //RSA  方式加密 
    //说明KEY必须是XML的行式,返回的是字符串
    //######################################################################
    //RSA的加密函数
    public string RSAEncrypt(string xmlPublicKey,string EncryptString )
    {
    byte[] PlainTextBArray;
    byte[] CypherTextBArray;
    string Result;
    System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
    rsa.FromXmlString(xmlPublicKey);
    PlainTextBArray = (new UnicodeEncoding()).GetBytes(EncryptString);
    CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
    //Result=(new UnicodeEncoding()).GetString(CypherTextBArray);
    Result=Convert.ToBase64String(CypherTextBArray);
    return  Result;
    }
    //RSA的解密函数
    public string  RSADecrypt(string xmlPrivateKey, string DecryptString )
    {
    byte[] PlainTextBArray;
    byte[] DypherTextBArray;
    string Result;
    System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
    rsa.FromXmlString(xmlPrivateKey);
    PlainTextBArray =Convert.FromBase64String(DecryptString);
    DypherTextBArray=rsa.Decrypt(PlainTextBArray, false);
    Result=(new UnicodeEncoding()).GetString(DypherTextBArray);
    return Result;
    }
    //RSA 的密钥产升
    //产生私钥 和公钥
    public void RSAKey(out string xmlKeys,out string  xmlPublicKey)
    {
    System.Security.Cryptography.RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
    xmlKeys=rsa.ToXmlString(true);
    xmlPublicKey = rsa.ToXmlString(false);
    }
      

  5.   

    老兄,为什么这一句总是报错呀???DypherTextBArray=rsa.Decrypt(PlainTextBArray, false);
      

  6.   

    另外我有一点不明白,下面这段程序是干什么用的,好像在前两个方法中并没有调用呀???
    public void RSAKey(out string xmlKeys,out string  xmlPublicKey)
    {
         RSACryptoServiceProvider rsa=new RSACryptoServiceProvider();
         xmlKeys=rsa.ToXmlString(true);
         xmlPublicKey = rsa.ToXmlString(false);
    }