How to implement a digital signature (dsacryptoserviceprovider) in Asp.net)

Source: Internet
Author: User

The process of digital signature is as follows:

Sender A sends data to receiver B. Sender a first performs hash function operations on the data to generate a summary of the information, and then encrypts the data with the private key to generate signature information, the information that a will send along with the signature information is sent to B. After receiving the information, B Also hashes the data and generates a summary of the information, then, use the public key and signature to verify whether the information has been written and modified.

Asp.net:

Namespace: using system. Security. cryptography needs to be introduced;

Reference content is as follows:
/// <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>
/// Generate a random key
/// </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 the 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 );
QCD. Core. Web. Messages. showdialog (Ret. tostring ());

}

HTML page code:

Reference content is as follows:
<Form ID = "form1" runat = "server">
Random key generation: <asp: button id = "btncreatemy" runat = "server" text = "random key generation" 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: & 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 signature"/>
<Br/>
<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>

 

Http://www.cn-web.com/shtml/article/net/quer/2009/07/28/921.shtml

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.