Use C # + ASP. NET for RSA (asymmetric) encryption.

Source: Internet
Author: User
Tags crypt

Encryption:

<% @ Page Language = "C #" autoeventwireup = "false" %>
<% @ Import namespace = "system. Security. Cryptography" %>
<% @ Import namespace = "system. Io" %>
<% @ Import namespace = "system. Text" %>

<%
String word = request. Params ["word"];
If (WORD = NULL ){
Response. Write ("no password entered !!! ");
Return;
}
 
Streamreader sr = new streamreader (@ "F:/a.txt", utf8encoding. utf8 );
String readpublickey = Sr. readtoend ();
Sr. Close ();
 
Rsacryptoserviceprovider crypt = new rsacryptoserviceprovider ();
Utf8encoding ENC = new utf8encoding ();
Byte [] bytes = enc. getbytes (Word );
Crypt. fromxmlstring (readpublickey );
Bytes = crypt. Encrypt (bytes, false );
String encryttext = convert. tobase64string (bytes );
String ABB = server. urlencode (encryttext );
Response. Write (ABB );

Response. Write ("<a href = 'take. aspx? WORD = "+ encryttext +" '> "+ encryttext + @" </a> ");
%>

Decryption:

<% @ Page Language = "C #" autoeventwireup = "false" %>
<% @ Import namespace = "system. Security. Cryptography" %>
<% @ Import namespace = "system. Io" %>
<% @ Import namespace = "system. Text" %>

<%
String word = request. querystring ["word"];
If (WORD = NULL ){
Response. Write ("no password is sent !!! ");
Return;
}
 
Streamreader sr = new streamreader (@ "F:/B .txt", utf8encoding. utf8 );
String readprivatekey = Sr. readtoend ();
Sr. Close ();

Response. Write ("<br>" + word );

Rsacryptoserviceprovider crypt = new rsacryptoserviceprovider ();
Utf8encoding ENC = new utf8encoding ();
Byte [] bytes = convert. frombase64string (@ word );
Crypt. fromxmlstring (readprivatekey );
Byte [] decryptbyte = crypt. decrypt (bytes, false );
String decrypttext = enc. getstring (decryptbyte );

Response. Write (decrypttext );

%>

The public key is in the.txt file, and the private key is in the B .txt file.

The method for making public and private keys is as follows:

Crypt = new rsacryptoserviceprovider ();
Publickey = crypt. toxmlstring (false); // Public Key
Privatekey = crypt. toxmlstring (true); // Private Key
Crypt. Clear ();

// Write to a text file
Streamwriter one = new streamwriter (@ "F:/a.txt", true, utf8encoding. utf8 );
One. Write (publickey );
Streamwriter two = new streamwriter (@ "F:/B .txt", true, utf8encoding. utf8 );
Two. Write (privatekey );
One. Flush ();
Two. Flush ();
One. Close ();
Two. Close ();
MessageBox. Show ("the public key and secret key are successfully saved! ");

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.