ASYMMETRIC ENCRYPTION RSA ALGORITHM OF ASYMMETRIC ENCRYPTION USED IN Asp.Net TOOLS

Keywords: C#

It has been several years since I entered the field of programmers. In the past few years, there have been gains (technical enhancements) and efforts (time and sub-health). Of course, there is no regret, the code is still a long way to go!!!

 

On The Way, never stop!!!

 

During the development process, I also accumulated some experience, code blocks and help classes, which really facilitated the later development process, but also shortened the development cycle. See the diagram for details (a small part). These are all recently written in line with common methods of development, and also used related projects. Now it seems a little bit exciting. !

             

Of course, I'm still a new person, and I need more advice from the old people in the garden, especially in code quality and technology. Your advice is very grateful, and it's also my way forward.  

 

Self-use Help Class

Prepare to explain and analyze in cycles, and hope you will all be encouraged!!!

 

Let's start with encryption and decryption today, haha.

 

In the development process, various encryption methods emerge endlessly, including symmetric encryption and asymmetric encryption; of course, passwords are also classified as reversible and irreversible.

 

In c #, asymmetric encryption is represented by RSA algorithm, which uses public key and private key to encrypt and decrypt.

 

In the process of encryption, the key length should be set to ensure the feasibility of the key length. Key length: 512,1024,2048,4096,8192.

 

The flow chart of RSA encryption algorithm is as follows:

 

1. First [the system] generates a pair of keys, i.e. private and public keys.

 

2. Then, [the system] sends the public key to [the user]

 

3. [User] Encrypts the data with the received public key and sends it to [system]

 

4. After receiving the data, the system decrypts it with its own private key and returns the password.

 

Because in asymmetric algorithm, the data encrypted by public key must be decrypted with the corresponding private key, and the private key can only be known by the receiver itself, thus ensuring the security of data transmission.

 

The theory is strong. The following code is demonstrated by a DEMO:

 

public static class RSA
{
/// <summary>
/// Generating key
/// <param name="PrivateKey">private key</param>
/// <param name="PublicKey">public key</param>
/// <param name="KeySize">Key length: 512,1024,2048,4096,8192</param>
/// </summary>
public static void Generator(out string PrivateKey, out string PublicKey, int KeySize = 2048)
{
  RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(KeySize);
  PrivateKey = rsa.ToXmlString(true); //take RSA The private key of the algorithm is exported to a string PrivateKey Medium parameter is true Represents the derived private key true Representation contains both RSA Public and private keys; false Represents that only public keys are included.
  PublicKey = rsa.ToXmlString(false); //take RSA The public key of the algorithm is exported to a string PublicKey Medium parameter is false Represents not exporting a private key true Representation contains both RSA Public and private keys; false Represents that only public keys are included.
}
/// <summary>
/// RSA Encryption imports public keys to RSA In the object, prepare for encryption
/// </summary>
/// <param name="PublicKey">public key</param>
/// <param name="encryptstring">String to be encrypted</param>
public static string RSAEncrypt(string PublicKey, string encryptstring)
{
  byte[] PlainTextBArray;
  byte[] CypherTextBArray;
  string Result;
  RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  rsa.FromXmlString(PublicKey);
  PlainTextBArray = (new UnicodeEncoding()).GetBytes(encryptstring);
  CypherTextBArray = rsa.Encrypt(PlainTextBArray, false);
  Result = Convert.ToBase64String(CypherTextBArray);
  return Result;
}
/// <summary>
/// RSA Decryption Imports Private Key RSA Prepare to decrypt
/// </summary>
/// <param name="PrivateKey">private key</param>
/// <param name="decryptstring">String to be decrypted</param>
public static string RSADecrypt(string PrivateKey, string decryptstring)
{
  byte[] PlainTextBArray;
  byte[] DypherTextBArray;
  string Result;
  RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  rsa.FromXmlString(PrivateKey);
  PlainTextBArray = Convert.FromBase64String(decryptstring);
  DypherTextBArray = rsa.Decrypt(PlainTextBArray, false);
  Result = (new UnicodeEncoding()).GetString(DypherTextBArray);
  return Result;
}
}
//Call method:
static void Main(string[] args)
{
  string PrivateKey = "";
  string PublicKey = "";
  RSA.Generator(out PrivateKey, out PublicKey, 1024);
  var aaa = RSA.RSAEncrypt(PublicKey, "123456789");
  var bbb = RSA.RSADecrypt(PrivateKey, aaa);
}

 

OK, officers, this issue of the article asymmetric encryption written here, thank you for your support, your support is my motivation!

Next issue will bring you several commonly used symmetric encryption methods, please look forward to!!!

 

Personal headlines: http://www.toutiao.com/c/user/3213034222/#mid=4129397771

Posted by nakins on Wed, 10 Apr 2019 20:57:31 -0700