Application of asymmetric encryption RSA and Its Implementation in C #

Source: Internet
Author: User
Tags asymmetric encryption

When it comes to data encryption, these words are often involved: algorithms, original texts, passwords, and keys. Generally, the sender uses an encryption algorithm to encrypt the original text and then sends it to the receiver. Then, the receiver decrypts the ciphertext with the key to obtain the original text. Since common encryption algorithms are public, the key to the original text encryption is the key. Encryption and decryption all use the same key algorithm, which is called symmetric encryption. symmetric encryption represents the des family. So what are the defects of this symmetric encryption? Since encryption and decryption use the same key, this key must be stored in at least two places. If encrypted data is sent to multiple users, more people will know the key, this greatly increases the risk of key leakage. If the key needs to be transmitted to the receiver by the sender, how to ensure the security of key transmission becomes another headache. To solve this problem, asymmetric encryption is introduced in comparison with symmetric encryption.

What is asymmetric encryption? Asymmetric encryption refers to a type of encryption algorithm that uses different keys for encryption and decryption. This type of encryption algorithm usually has two keys, A and B. The ciphertext obtained from key a's encrypted data can only be decrypted by key B (even key a cannot be decrypted). On the contrary, only key a can decrypt the ciphertext obtained by encrypting data with key B. These two keys are called private keys and public keys respectively. As the name suggests, private keys are private keys that you reserve and cannot be disclosed, while public keys are published to the other side of encryption and decryption operations. For different purposes, the keys used for data encryption are also different (sometimes public key encryption and Private Key decryption; sometimes private key encryption and Public Key decryption ). Asymmetric encryption indicates the RSA algorithm.

So what are the purposes of asymmetric encryption?

The first purpose is data encryption.

Imagine which key is used for data encryption if I don't want anyone except the recipient to know the content of the data I sent? If I use private key encryption, the receiver needs to use the public key for decryption Based on the asymmetric encryption principle, and the public key has been published to the receiver. This solution seems feasible, however, this problem occurs on the public key. In asymmetric encryption, Public Key Disclosure not only refers to the disclosure of the receiver, but also to the thorough disclosure of the key, which can be obtained by anyone who needs it, in this way, there will be no secret to the data you send. In turn, if I use the public key to encrypt data, the receiver needs to use the private key for data decryption. Because the private key is only stored in the receiver, in this way, other people will not get the data content. In this case, in asymmetric encryption, if you need to protect your data from being obtained by a third party, the key must be generated by the receiver, and then the receiver discloses the public key, the sender uses the public key to encrypt the data and transmit it to the receiver. The receiver uses its own private key for decryption, thus ensuring data security. Therefore, asymmetric encryption is also called public key encryption.

The execution efficiency of asymmetric encryption is much lower than that of symmetric encryption, so we do not use asymmetric encryption algorithms for a large file or data. How can we encrypt a large file? In general, we can select a symmetric encryption algorithm to encrypt the file, and then use an asymmetric algorithm to encrypt the key of the symmetric algorithm, so as to ensure the security of the symmetric algorithm key transfer.

Another purpose of asymmetric encryption is to sign digital signatures.

What is a digital signature? The digital signature is the same as our signature on the contract. The receiver can use it to prove that the received data or file was sent by you. For example, if the sender needs to send a string of data d to the receiver, how does the receiver determine that data D is sent by the sender?

Step 1: The sender generates a pair of keys and discloses the public key to the receiver;

Step 2: The sender encrypts data D with the private key to obtain the ciphertext m, and then sends the data d and the ciphertext M together to the receiver;

Step 3: The receiver obtains the data d and ciphertext m, and then uses the public key to decrypt the ciphertext m to obtain the data d;

Step 4: Compare D and D. If D is equal, it indicates that D is sent by the sender.

In practice, we do not directly use the private key to encrypt the data or files to be sent. This is because the asymmetric encryption algorithm is very time-consuming and the ciphertext length must be greater than the plaintext length, the overhead of the direct encryption system is very high. So how can we solve this problem?

First, we need to understand another concept: Message Digest. The so-called message digest is the only fixed-length value corresponding to a file or data calculated by a one-way algorithm. It is also called a digital digest. According to different algorithms, the message digest length is generally 128 bits or 160 bits. Common message digest algorithms include MD5 and sha1. The message digest of a file is the same as the fingerprint of a person. It can uniquely represent this file. If this file is modified, its message digest will change. Therefore, we can sign the message digest of a file instead of itself. In addition, we can verify the message digest to determine whether the sent data is complete or has been modified. In this way, the above example can be roughly changed to the following:

Step 1: The sender generates a pair of keys and discloses the public key to the receiver;

Step 2: The sender abstracts the data d and obtains Q;

Step 3: encrypt Q with the private key to obtain the ciphertext MQ, and then send the data d and the ciphertext MQ together to the receiver;

Step 4: The receiver obtains data d and ciphertext MQ, and uses the public key to decrypt the ciphertext MQ to obtain Q1;

Step 5: the receiver uses the same algorithm to digest the data d and obtain Q2;

Step 6: Compare Q1 and Q2. If yes, it means that D is sent by the sender and has not been modified.

Now, we have introduced a lot about the use of asymmetric encryption. Next we will look at how to use C # To implement RSA asymmetric encryption.

.. Net. security. cryptography, so please add using system in your program first. security. cryptography. The RSA encryption algorithm is implemented by the rsacryptoserviceprovider object. The following code implements data encryption, decryption, signature, and verification.

   1: string PublicKey, PrivateKey;

   2: RSACryptoServiceProvider rsaProvider;

   3: void Initial()

   4: { 

5: // declare an RSA algorithm instance. The length of the key is 1024 characters specified by the rsacryptoserviceprovider Type constructor.

6: // After rsacryptoserviceprovider is instantiated, rsacryptoserviceprovider automatically generates key information.

   7:     rsaProvider = new RSACryptoServiceProvider(1024);

8: // export the public key of the RSA algorithm to the publickey string. If the parameter is set to false, the private key is not exported.

   9:     PublicKey = rsaProvider.ToXmlString(false);

10: // export the private key of the RSA algorithm to the privatekey string. If the parameter is true, the private key is exported.

  11:     PrivateKey = rsaProvider.ToXmlString(true);

  12:  

  13:  

  14: byte[] EncryptData(byte[] data)

  15: {

  16:     RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);

17: // import the public key to the RSA object and prepare for encryption;

  18:     rsa.FromXmlString(PublicKey);

19: // encrypt data and return the encrypted result;

20: // The second parameter is used to select the padding format.

  21:     return rsa.Encrypt(data, false);

  22: }

  23:  

  24: byte[] DecryptData(byte[] data)

  25: {

  26:     RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);

27: // import the private key to RSA for decryption;

  28:     rsa.FromXmlString(PrivateKey);

29: // decrypt the data and return the decryption result;

  30:     return rsa.Decrypt(data, false);

  31: }

  32:  

  33: byte[] Sign(byte[] data)

  34: {

  35:     RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);

36: // import the private key and prepare the signature

  37:     rsa.FromXmlString(PrivateKey);

38: // use MD5 to digest the message, sign the digest, and return the signature data.

  39:     return rsa.SignData(data, "MD5");

  40: }

  41:  

  42: bool Verify(byte[] data, byte[] Signature)

  43: {

  44:     RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(1024);

45: // import the public key to verify the signature

  46:     rsa.FromXmlString(PublicKey);

47: // return the data verification result

  48:     return rsa.VerifyData(data, "MD5", Signature);

  49: }

 

For more information about RSA, refer to the msdn and online tutorials to start your own exercises. I will continue to explain how to save the key in later articles. I hope you will like it.

In addition, I hope you can find out the specific RSA Algorithms on the Internet. I really don't know what they are carrying in their heads ~~

 

Technorati tags: RSA, C #,. net, digital certificate, asymmetric encryption

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.