Encryption and decryption algorithm two: asymmetric plus decryption

Source: Internet
Author: User
Tags asymmetric encryption

  Encrypt and decrypt a class of cryptographic algorithms that use different keys. This type of encryption algorithm usually has two keys A and B, using key a encryption data obtained ciphertext, only key B can be decrypted operation (even if key a cannot be decrypted);
Instead, the ciphertext that is obtained by encrypting the data with key B can only be decrypted by the key A. These two keys are called private keys and public keys, respectively. A private key is a key that you keep, not public, and the public key is publicly available to the other side of the encryption and decryption operation.
Depending on the purpose, the key used to encrypt the data is also different (sometimes with public key encryption, private key decryption ; Sometimes the opposite is encrypted with the private key, and the public key is decrypted ). The representative algorithm of asymmetric encryption is the RSA algorithm.
  

The RSA algorithm is the first algorithm that can be used both for data encryption and for digital signatures. It is easy to understand and operate, and very popular. Its security is based on the difficulty of large integer factor decomposition, and the large integer factorization problem is a famous mathematical problem, so far there is no effective method to solve it, so the security of RSA algorithm can be ensured.

The implementation of RSA algorithm is divided into three parts: the generation of public key and private key, asymmetric encryption and decryption implementation, digital signature and verification.

  

generation of public and private keys:
        stringPublicKey, Privatekey;        RSACryptoServiceProvider Rsaprovider; voidInitial () {//declares an instance of an RSA algorithm that is specified by the constructor of the RSACryptoServiceProvider type with a key length of 1024 bits//RSACryptoServiceProvider automatically generates key information when RSACryptoServiceProvider is instantiatedRsaprovider =NewRSACryptoServiceProvider (1024x768); //exports the public key of the RSA algorithm to the string publickey, true means that both the RSA public key and the private key are included; false means that only the public key is includedPublicKey = Rsaprovider. Toxmlstring (false); //exports the private key of the RSA algorithm to the string Privatekey, true to include both the RSA public key and the private key; false means that only the public key is includedPrivatekey = Rsaprovider. Toxmlstring (true); }
Asymmetric encryption and decryption implementations:

Public key cryptography, private key decryption (private key encryption, public key decryption algorithm):

byte[] EncryptData (byte[] data) {RSACryptoServiceProvider RSA=NewRSACryptoServiceProvider (1024x768); //Import the public key into the RSA object and prepare the encryption;RSA.            Fromxmlstring (PublicKey); //The data is encrypted and the encrypted result is returned;//The second parameter is used to select the format of the padding            returnRsa. Encrypt (data,false); }        byte[] Decryptdata (byte[] data) {RSACryptoServiceProvider RSA=NewRSACryptoServiceProvider (1024x768); //Import the private key into RSA and prepare to decrypt;RSA.            Fromxmlstring (Privatekey); //decrypts the data and returns the decrypted result;            returnRsa. Decrypt (data,false); }

Encryption and decryption algorithm two: asymmetric plus decryption

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.