. NET encryption and decryption--encryption mode of asymmetric encryption

Source: Internet
Author: User
Tags decrypt modulus asymmetric encryption


One, asymmetric encryption


Asymmetric encryption has a set of key pairs, one is public, anyone can obtain, called the public key;

One is self-preservation and strictly confidential, called the private key.



The rules are as follows:

by someone A the public key encrypted message, only by the A the private key is decrypted;

by A The private key encrypts the message, only by the A decryption of the public key.

sender, the receiver holds the public key / private key pair, so there will be a total of four keys. The advantage of non-heap encryption is that the private key is held by itself and the public key is fully exposed.



two, encryption mode


Steps:

1 , the receiver publishes its own public key, which can be obtained by anyone.

2 , the sending party uses Receiving Party the public key of the message is encrypted and then sent.

3 , Receiving Party Use own the private key to decrypt the message.

Disadvantages:

1 , it is not possible to determine who sent the message.



three, encryption mode demo



/*//Encryption Mode */namespace Asymmetric Encryption {class Program {static void Main (string[] args) {string pla            InText = "Hello World"; String PublicKey = "<rsakeyvalue><modulus>qa89wuhlcmvyhjqw+ Mfjrz6ep8xuicvrkvwkuufrhbamlgxt2lhthssbszhfeechqvwgff+oybgj1ki72a3h056tm6yrnppjfajggrnsjswppna14e6f+zbvc/ mzrislgntdxhbhuvnsmx+hlu+skz+b75rcmoyueoup8gxfeqs=</modulus><exponent>aqab</exponent></            Rsakeyvalue> ";  String encryptedtext = Rsacryptohelper.encrypt (PublicKey, plaintext);            Confidential ~ ~ Console.WriteLine (encryptedtext); String Privatekey = "<rsakeyvalue><modulus>qa89wuhlcmvyhjqw+ Mfjrz6ep8xuicvrkvwkuufrhbamlgxt2lhthssbszhfeechqvwgff+oybgj1ki72a3h056tm6yrnppjfajggrnsjswppna14e6f+zbvc/ Mzrislgntdxhbhuvnsmx+hlu+skz+b75rcmoyueoup8gxfeqs=</modulus><exponent>aqab</exponent><p >7vxysshaimztnvzgk3h3u9llnzsa5mck4/ilvatq5h3+yhegt0t+q2tv844quxcbpvkkrf+uvsb043nw65kotw==</p><q>tug739ddgwvrsbxle1zmuabvbzeunuicosbcgp/lsmbscdnk46rifjvz3nglfptbav4i7mpatr8je1o5gl485q==</q><dp> Zyfxnjuyhxuilxzcjcccwb88pvklflceqb0nia1kaqihwjxreakkt/f0vfnk3mvbclyx/bk6ua7egktfrcub+w==</dp><dq> rlubbingbo6/hfjbz6gcpcec4ebyb7s02dygjxzfsyejdhq3a7taw+qn4keshk6cmirrbu7qpjmdvzk3r1wr/q==</dq>< inverseq>a0q3ffhjshdazw0qrkqznunsq+j5/ltps9zajqivho2abayagwkavvsbud7cb+i4easaw4uqhrk456vkw/hqnw==</ inverseq><d>dyxifvafc2jrctd8mkw6e2ttsf6iha1t5y6t+xc5jvd7t/ Yi0qg7ce23bt1tpchc0hgdlstqjs3hgxzx6yjez0frz37uunnsnyrhh3canxquawaczmf7tpyoqbmgxp5ofpgaijumhmwysmm9wdrtoce6h39t5qlly7g6xh9            Mj+k=</d></rsakeyvalue> ";  String cleartext = Rsacryptohelper.decrypt (Privatekey, encryptedtext);            Decryption Console.WriteLine (cleartext);        Console.readkey (); }} public class Rsacryptohelper {#region Key acquisition method-not encapsulated in//method one://string privat ekey= provider. Toxmlstring (TRUE);//Get public/private key pair//String publickey= provider. Toxmlstring (false);//Get public key pair//Method two://rsaparameters Privatekey = provider. Exportparameters (TRUE);//Get the public key to//rsaparameters PublicKey = provider.        Exportparameters (false);//Get public key #endregion///<summary>//Sender encryption///</summary> <param name= "Publickeyxml" >the public key xml.</param>//<param name= "plaintext" >the Plain text.</param>//<returns>System.String.</returns>//<remarks>editor:v-liuh CH createtime:2015/5/16 22:06:54</remarks> public static string Encrypt (string publickeyxml, string plaintext            ) {RSACryptoServiceProvider Provider = new RSACryptoServiceProvider (); Provider. Fromxmlstring (Publickeyxml);            Initialize object with public key byte[] Plaindata = Encoding.Default.GetBytes (plaintext); byte[] EncryptedData = provider. Encrypt (Plaindata, true);//Encrypt data REturn convert.tobase64string (EncryptedData);  }///<summary>////</summary>//<param name= "Privatekeyxml" >the Private key xml.</param>//<param name= "Encryptedtext" >the encrypted text.</param>// <returns>System.String.</returns>//<remarks>editor:v-liuhch createtime:2015/5/16 22:11:09< /remarks> public static string Decrypt (String privatekeyxml, String encryptedtext) {Rsacryptoservice            Provider Provider = new RSACryptoServiceProvider (); Provider.            Fromxmlstring (privatekeyxml);//Use the private key to initialize the data byte[] EncryptedData = convert.frombase64string (encryptedtext); byte[] Plaindata = provider.  Decrypt (EncryptedData, true);  Decrypt the data string plaintext = Encoding.Default.GetString (plaindata);                Clear text return plaintext; }    }}











. NET encryption and decryption--encryption mode of 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.