C # based on large integer class RSA algorithm implementation (public key encryption decryption, private key encryption decryption)

Source: Internet
Author: User
Tags rand

Recently, because the project needs to use RSA encryption to ensure that the client and server communication security. But C # 's own RSA algorithm class RSACryptoServiceProvider only supports public key cryptographic private key decryption, that is, the use of digital certificates.

So reference some of the information on the Internet to write a RSA algorithm implementation. The algorithm implementation is based on a large integer class provided on the Internet.

First, Key management

The key to getting keys is 2 ways

One is obtained by 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);
}

A method of obtaining large prime numbers by means of BigInteger

///&lt;summary&gt;


///obtains the key pair


///&lt;/summary&gt;


&lt;param name= "n" &gt; Large integer &lt;/param&gt;


///&lt;param name= "E" &gt; Public key &lt;/param&gt;


///&lt;param name= "D" &gt; Key &lt;/param&gt;


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 (A., Rand);


BigInteger bi_d = Bi_e.modinverse (BI_PQ);


n = bi_n.tohexstring ();


e = bi_e.tohexstring ();


d = bi_d.tohexstring ();


        }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.