日期:2014-05-17  浏览次数:20969 次

RSA私钥加密,公钥解密算法(请看详细再回答,不会的帮顶下,谢谢)
各位达人,小弟最近在做项目时候遇到一个关于RSA加密解密的问题。大家都知道.NET库提供的RSACryptoServiceProvider类只支持公钥加密,私钥解密,而现在我遇到的事情是需要私钥加密公钥解密。而我所做的只是公钥解密(公私钥对是java生成的,只提供给我公钥)。针对这个问题,我也在网上找了一些例子,但是还是没有实现。我在网上找到了一个C#写的BigInteger类,并且按照

  /// <summary>
        /// 通过公钥解密
        /// </summary>
        /// <param name="dataBytes">待解密字符数组</param>
        /// <returns>解密结果</returns>
        public string DecryptByPublicKey(byte[] dataBytes, byte[] keyN, byte[] keyE)
        {
            //大整数N
            BigInteger biN = new BigInteger(keyN);
            //公钥大素数
            BigInteger biE = new BigInteger(keyE);
            //解密
            return DecryptBytes(dataBytes, biE, biN);
        }

  /// <summary>
        /// 解密字符数组
        /// </summary>
        /// <param name="dataBytes">待解密字符数组</param>
        /// <param name="KeyNum">密钥大素数</param>
        /// <param name="nNum">大整数N</param>
        /// <returns>解密结果</returns>
        private string DecryptBytes(byte[] dataBytes, BigInteger KeyNum, BigInteger nNum)
        {
            int len = dataBytes.Length;
            int len1 = 0;
            int blockLen = 0;
            if (len % 128 == 0)
            {
                len1 = len / 128;
            }
            else
            {
                len1 = len / 128 + 1;
            }
            List<byte> tempbytes = new List<byte>();
            for (int i = 0; i < len1; i++)
            {
                if (len >= 128)
                {
                    blockLen = 128;
                }
                else
                {
                    blockLen = len;
                }
                byte[] oText = new byte[blockLen];
                Array.Copy(dataBytes, i * 128, oText, 0, blockLen);
                BigInteger biText = new BigInteger(oText);
                BigInteger biEnText = biText.modPow(KeyNum, nNum);
                byte[] testbyte= biEnText.getBytes();
                string str = Encoding.UTF8.GetString(testbyte);
                tempbytes.AddRange(testbyte);
                len -= blockLen;
            }
            return System.Text.Encoding.UTF8.GetString(tempbytes.ToArray());
        }

我调用的方法是:
  private void DecryptByPublicKeyTest()
  {
  //key
  byte[] key = Encoding.UTF8.GetBytes("12345");
  //用私钥加密key
  RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  byte[] desc = rsa.Encrypt(key, false);
  string getsource = Encoding.UTF8.GetString(rsa.Decrypt(desc, false));
  RSAParameters para = rsa.ExportParameters(false);

  string gets = DecryptByPublicKey(desc, para.Modulus, para.Exponent);
  }

但是解密出来的跟原来的“12345”不一样。

------解决方案--------------------
C# code

  byte[] Code = System.Text.Encoding.UTF8.GetBytes("1232324");
         
            RSACryptoServiceProvider TempRSA = new RSACryptoServiceProvider();
            RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();
       
            //导入私匙 来加密
            RSA.ImportParameters(TempRSA.ExportParameters(false));
            byte[] EnCode = RSA.Encrypt(Code, false);
          
            //导入公匙来解密
            RSA.ImportParameters(TempRSA.ExportParameters(true));
            byte[] DeCode = RSA.Decrypt(EnCode, false);