Digital signature instance

Source: Internet
Author: User

I have read a lot about the specific benefits of digital signatures.ArticleVarious diagrams on the network may be difficult to understand. Here, I will briefly talk about the principles to reduce misunderstandings. This is my personal understanding. Please correct them:

A sends a message to B. A encrypts the information with the password of A, sends the encrypted string together with the original text to B, and then B decrypts it with the password of B, then, determine whether the decrypted string is consistent with the original text sent by A. The key issue is,
The passwords A and B are different. This is the essence of digital signatures. A's password is the private key, and B's password is the public key.

Procedure:

When a private key and public key are generated, A uses the private key for encryption. Because the private key of A is only owned by a, the encrypted string is the signature string of, then a sends the signature string and the original text to B,

B obtains the encrypted string and decrypts it with the public key. Then, it determines whether the decrypted string is consistent with the original one. It indicates that it is signed by a. Otherwise, it is not signed by.
 

If you are still confused, you can see the following example and the text above.

For example, if the private key is S1 and the Public Key is G1 A in the original text: 123, then the process is
S1 + 123 encrypt to generate a signature string: axxxx
A sends axxxx and 123 together to B, B decrypts axxxx with G1, and determines whether the decrypted string is equal to 123.

 

I believe that everyone understands the principles... Just understand the principle ,. Haha... Let's not talk about anything else.Code... Code is to learn from others .. Tested.

 

 

Aspx code:

<Form ID ="  Form1  " Runat = "  Server  " > Generate random keys: <Asp: button id = "  Btncreatemy  " Runat = "  Server  " TEXT = "  Random generation of keys  " Onclick = "  Btncreatemy_click  " /> <Br/> Public Key: <Asp: textbox id = "  Tbxcreatemy_publickey  " Runat = "  Server  " Textmode = "  Multiline  " Height = "  59px " Readonly = "  True  " Width = "  711px  " > </ASP: textbox> <br/> Private Key: <Asp: textbox id = "  Tbxcreatemy_key  " Runat = "  Server  " Textmode = " Multiline  " Height = "  59px  " Readonly = "  True  " Width = "  710px  " > </ASP: textbox> <br/> <HR/> <br/> Generate a signature: <Br/> Original article: & Nbsp; <Asp: textbox id ="  Tbxcontent  " Runat = "  Server  " Textmode = "  Multiline  " Height = "  59px  " Width = "  711px  " > </ASP: textbox> & nbsp; <br/>Private Key: <Asp: textbox id = "  Tbxkey  " Runat = "  Server  " Textmode = "  Multiline  " Height = "  59px  " Width = "  711px  " > </ASP: textbox> <br/> Signature: <Asp: textbox id = "  Tbxsign  " Runat = "  Server  " Textmode = "  Multiline  " Height = "  59px  " Readonly = "  True " Width = "  711px  " > </ASP: textbox> <br/> <asp: button id = "  Button1  " Runat = "  Server  " Onclick = "  Button#click  " TEXT = "  Generate a signature " /> <Br/> <HR/> <br/> Verify the signature: <Br/> Original article: <Asp: textbox id = "  Tbxcontentyz  " Runat = "  Server  " Textmode = "  Multiline  " Height = "  59px " Width = "  711px  " > </ASP: textbox> <br/> Public Key: <Asp: textbox id = "  Tbxpublickeyyz  " Runat = "  Server  " Textmode = "  Multiline  " Height = " 59px  " Width = "  711px  " > </ASP: textbox> <br/> Signature: <Asp: textbox id = "  Tbxsignyz  " Runat = "  Server  " Textmode = "  Multiline  " Height ="  59px  " Width = "  711px  " > </ASP: textbox> <br/> <asp: button id = "  Button3  " Runat = "  Server  " Onclick = "  Button3_click  " TEXT ="  Verify signature  " /> </Form>

. CS code

   ///   <Summary>          ///  Generate a signature  ///   </Summary>          ///   <Param name = "sender"> </param>          ///   <Param name = "E"> </param>          Protected   Void Button#click ( Object  Sender, eventargs e) {dsacryptoserviceprovider objdsa = New  Dsacryptoserviceprovider (); objdsa. fromxmlstring (tbxkey. Text );  Byte [] Source = System. Text. utf8encoding. utf8.getbytes (tbxcontent. Text );  //  Digital Signature Tbxsign. Text = Bitconverter. tostring (objdsa. signdata (source ));}  ///  <Summary>          ///  Random generation of keys  ///   </Summary>          ///   <Param name = "sender"> </param>          ///   <Param name = "E"> </param>          Protected   Void Btncreatemy_click ( Object  Sender, eventargs e) {dsacryptoserviceprovider objdsa = New Dsacryptoserviceprovider (); tbxcreatemy_publickey.text = Objdsa. toxmlstring ( False  ); Tbxcreatemy_key.text = Objdsa. toxmlstring ( True  );}  ///   <Summary>          ///  Verify signature  ///   </Summary>          ///   <Param name = "sender"> </param>         ///   <Param name = "E"> </param>          Protected   Void Button3_click ( Object  Sender, eventargs e) {dsacryptoserviceprovider objdsa = New  Dsacryptoserviceprovider ();  Byte [] Filehashvalue = New  Sha1cryptoserviceprovider (). computehash (system. Text. utf8encoding. utf8.getbytes (tbxcontentyz. Text ));  String [] Strsplit = tbxsignyz. Text. Split ( '  -  '  );  Byte [] Signedhash = New   Byte  [Strsplit. Length];  For ( Int I = 0 ; I <strsplit. length; I ++ ) Signedhash [I] = Byte . Parse (strsplit [I], system. Globalization. numberstyles. allowhexspecifier); objdsa. fromxmlstring (tbxpublickeyyz. Text );  Bool Ret = Objdsa. verifysignature (filehashvalue, signedhash); response. Write (Ret. tostring ());  //  QCD. Core. Web. Messages. showdialog (Ret. tostring ());  } 

 

Related Article

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.