C#基於大整數類的RSA演算法實現(公開金鑰加密解密,私密金鑰加密解密)

來源:互聯網
上載者:User

最近因為項目需要通過RSA加密來保證用戶端與服務端的通訊安全。但是C#自 帶的RSA演算法類RSACryptoServiceProvider只支援公開金鑰加密私密金鑰解密,即數字證 書的使用。

所以參考了一些網上的資料寫了一個RSA的演算法實現。演算法實 現是基於網上提供的一個大整數類。

一、密鑰管理

取得密鑰主要 是通過2種方式

一種是通過RSACryptoServiceProvider取得:

/// <summary>
/// RSA演算法對象,此處主要用於擷取金鑰組
/// </summary>
private RSACryptoServiceProvider RSA;

/// <summary>
/// 取得密鑰
/// </summary>
/// <param name="includPrivateKey">true:包含私密金鑰   false:不包含私密金鑰</param>
/// <returns></returns>
public string ToXmlString(bool includPrivateKey)
{
      if (includPrivateKey)
      {
             return RSA.ToXmlString(true);
      }
      else
      {
            return RSA.ToXmlString(false);
      }
}

/// <summary>
/// 通過密鑰初始化RSA對象
/// </summary>
/// <param name="xmlString">XML格式的密鑰資訊 </param>
public void FromXmlString(string xmlString)
{
      RSA.FromXmlString(xmlString);
}

一種是通過BigInteger中的擷取大素數的方法

/// <summary>
        /// 取得金鑰組
        /// </summary>
        /// <param name="n">大整數 </param>
        /// <param name="e">公開金鑰 </param>
        /// <param name="d">密鑰 </param>
        public void GetKey(out string n,out string e,out string d )
        {
            byte[] pseudoPrime1 = {
                        (byte)0x85, (byte)0x84, (byte)0x64, (byte)0xFD, (byte)0x70, (byte)0x6A,
                        (byte)0x9F, (byte)0xF0, (byte)0x94, (byte)0x0C, (byte)0x3E, (byte)0x2C,
                        (byte)0x74, (byte)0x34, (byte)0x05, (byte)0xC9, (byte)0x55, (byte)0xB3,
                        (byte)0x85, (byte)0x32, (byte)0x98, (byte)0x71, (byte)0xF9, (byte)0x41,
                        (byte)0x21, (byte)0x5F, (byte)0x02, (byte)0x9E, (byte)0xEA, (byte)0x56,
                        (byte)0x8D, (byte)0x8C, (byte)0x44, (byte)0xCC, (byte)0xEE, (byte)0xEE,
                        (byte)0x3D, (byte)0x2C, (byte)0x9D, (byte)0x2C, (byte)0x12, (byte)0x41,
                        (byte)0x1E, (byte)0xF1, (byte)0xC5, (byte)0x32, (byte)0xC3, (byte)0xAA,
                        (byte)0x31, (byte)0x4A, (byte)0x52, (byte)0xD8, (byte)0xE8, (byte)0xAF,
                        (byte)0x42, (byte)0xF4, (byte)0x72, (byte)0xA1, (byte)0x2A, (byte)0x0D,
                        (byte)0x97, (byte)0xB1, (byte)0x31, (byte)0xB3,
                };

            byte[] pseudoPrime2 = {
                        (byte)0x99, (byte)0x98, (byte)0xCA, (byte)0xB8, (byte)0x5E, (byte)0xD7,
                        (byte)0xE5, (byte)0xDC, (byte)0x28, (byte)0x5C, (byte)0x6F, (byte)0x0E,
                        (byte)0x15, (byte)0x09, (byte)0x59, (byte)0x6E, (byte)0x84, (byte)0xF3,
                        (byte)0x81, (byte)0xCD, (byte)0xDE, (byte)0x42, (byte)0xDC, (byte)0x93,
                        (byte)0xC2, (byte)0x7A, (byte)0x62, (byte)0xAC, (byte)0x6C, (byte)0xAF,
                        (byte)0xDE, (byte)0x74, (byte)0xE3, (byte)0xCB, (byte)0x60, (byte)0x20,
                        (byte)0x38, (byte)0x9C, (byte)0x21, (byte)0xC3, (byte)0xDC, (byte)0xC8,
                        (byte)0xA2, (byte)0x4D, (byte)0xC6, (byte)0x2A, (byte)0x35, (byte)0x7F,
                        (byte)0xF3, (byte)0xA9, (byte)0xE8, (byte)0x1D, (byte)0x7B, (byte)0x2C,
                        (byte)0x78, (byte)0xFA, (byte)0xB8, (byte)0x02, (byte)0x55, (byte)0x80,
                        (byte)0x9B, (byte)0xC2, (byte)0xA5, (byte)0xCB,
                };


            BigInteger bi_p = new BigInteger (pseudoPrime1);
            BigInteger bi_q = new BigInteger (pseudoPrime2);
            BigInteger bi_pq = (bi_p - 1) * (bi_q - 1);
            BigInteger bi_n = bi_p * bi_q;
            Random rand = new Random();
            BigInteger bi_e = bi_pq.genCoPrime(512, rand);
            BigInteger bi_d = bi_e.modInverse(bi_pq);
            n = bi_n.ToHexString();
            e = bi_e.ToHexString();
            d = bi_d.ToHexString();
        }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.