Encryption, password unauthentication, Signature Verification

Source: Internet
Author: User

 

Using System;
Using System. Data;
Using System. configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. Web. UI. webcontrols. webparts;
Using System. Web. UI. htmlcontrols;
Using System. Security;
Using System. Security. cryptography;
Using System. text;
Namespace Cryption
{
Public   Class Cryption
{

# RegionDifferences between convert. tobase64string and encoding. getstring
/*
*
Both convert byte [] to a string. The difference is that convert. tobase64string uses Base 64 numeric encoding, so it generates all ASCII characters.

However, encoding. getstring is a string that we generally know.

Some encryption methods return byte []. Which one should we use to convert the data to a string?

Using encoding. getstring conversion, you may get many squares or question marks. This is because some integer sequences cannot correspond to the text in our real life. They can only be replaced by squares or question marks.

Therefore, we generally use convert. tobase64string.

On the other hand, which one can be used to convert a string to a byte? Must I use frombase64string?

No. Our common text (such as string type) is not base64-encoded and cannot be converted to byte [] Using frombase64string, for example: frombase64string ("Qianyi network ") an error occurs, because the "Qianyi network" is not base64-encoded. In this case, encoding should be used. getbytes ().

*/

# Endregion

///   <Summary>
/// Obtain the public key and private key.
///   </Summary>
///   <Param name = "publickey"> </param>
///   <Param name = "privatekey"> </param>
Public   Static   Void Getkey ( Out   String Publickey, Out   String Privatekey)
{
Rsacryptoserviceprovider rsaprovider =   New Rsacryptoserviceprovider ();
Publickey = Rsaprovider. toxmlstring ( False );
Privatekey = Rsaprovider. toxmlstring ( True );
}

///   <Summary>
/// Add Password
///   </Summary>
///   <Param name = "publickey"> </param>
///   <Param name = "encrypttext"> </param>
///   <Returns> </returns>
Public   Static   String Encrypt ( String Publickey, String Encrypttext)
{

Byte [] Byteencrypttext =   New Unicodeencoding (). getbytes (encrypttext );
Rsacryptoserviceprovider rsprovider =   New Rsacryptoserviceprovider ();
Rsprovider. fromxmlstring (publickey );
Return Convert. tobase64string (rsprovider. Encrypt (byteencrypttext, False ));

}

///   <Summary>
/// Decryption
///   </Summary>
///   <Param name = "privatekey"> </param>
///   <Param name = "decrypttext"> </param>
///   <Returns> </returns>
Public   Static   String Decrypt ( String Privatekey, String Decrypttext)
{
Unicodeencoding uni =   New Unicodeencoding ();
Byte [] Byteencrypttext = Convert. frombase64string (decrypttext );
Rsacryptoserviceprovider rsprovider =   New Rsacryptoserviceprovider ();
Rsprovider. fromxmlstring (privatekey );
Return Uni. getstring (rsprovider. decrypt (byteencrypttext, False ));

}

///   <Summary>
/// Calculate the hash value of a specified byte array
///   </Summary>
///   <Param name = "text"> </param>
///   <Returns> </returns>
Public   Static   String Gethash ( String Text)
{
Byte [] Bytetext =   New Unicodeencoding (). getbytes (text );
Hashalgorithm ha = Hashalgorithm. Create ( " MD5 " );
Return Convert. tobase64string (Ha. computehash (bytetext ));
}

///   <Summary>
/// Create a signature
///   </Summary>
///   <Param name = "publickey"> </param>
///   <Param name = "text"> </param>
///   <Param name = "signaturetext"> Signed Data </Param>
///   <Returns> </returns>
Public   Static   Void Signatureformatter ( String Privatekey, String Text, Ref   String Signaturetext)
{

Byte [] Bytetext = Convert. frombase64string (text );
Rsacryptoserviceprovider rsaprovider =   New Rsacryptoserviceprovider ();
Rsaprovider. fromxmlstring (privatekey );
Rsapkcs1signatureformatter rsasignatureformatter =   New Rsapkcs1signatureformatter (rsaprovider );
Rsasignatureformatter. sethashalgorithm ( " MD5 " );
Byte [] Bytesignaturetext = Rsasignatureformatter. createsignature (bytetext );
Signaturetext = Convert. tobase64string (bytesignaturetext );

}

///   <Summary>
/// Verify signature
///   </Summary>
///   <Param name = "key"> </param>
///   <Param name = "text"> Original Text </Param>
///   <Param name = "signaturetext"> Signed text </Param>
///   <Returns> True: Correct; false: Error </Returns>
Public   Static   Bool Signaturedeformatter ( String Publickey, String Text, String Signaturetext)
{
Byte [] Bytetext = Convert. frombase64string (text );
Byte [] Bytesignaturetext = Convert. frombase64string (signaturetext );
Rsacryptoserviceprovider rsaprovider =   New Rsacryptoserviceprovider ();
Rsaprovider. fromxmlstring (publickey );
Rsapkcs1signaturedeformatter rsasignaturedeformatter =   New Rsapkcs1signaturedeformatter (rsaprovider );
Rsasignaturedeformatter. sethashalgorithm ( " MD5 " );
Return Rsasignaturedeformatter. verifysignature (bytetext, bytesignaturetext );
}

}
}

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.