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