(C # Foundation) various cryptographic learning

Source: Internet
Author: User

Before, always want to do an understanding of this, but always insist on not go on, very impatient. Recently read a few times articles, very touched, so again to start learning, from the most basic start-the so-called "slow is fast." The mentality has changed, go on! On the code!

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Security.Cryptography;namespacedazilianxi.wenjian{ Public classRsacryptohelper {//Encrypt         Public Static stringEncrypt (stringPublickeyxml,stringplaintext) {RSACryptoServiceProvider provider=NewRSACryptoServiceProvider (); Provider. Fromxmlstring (Publickeyxml); //initializing an object with a public key            byte[] Plaindata =Encoding.Default.GetBytes (plaintext); byte[] EncryptedData = provider. Encrypt (Plaindata,true); returnconvert.tobase64string (EncryptedData); }        //decryption         Public Static stringDecrypt (stringPrivatekeyxml,stringencryptedtext) {RSACryptoServiceProvider provider=NewRSACryptoServiceProvider (); Provider.            Fromxmlstring (Privatekeyxml); byte[] EncryptedData =convert.frombase64string (Encryptedtext); byte[] Plaindata = provider. Decrypt (EncryptedData,true); stringplaintext =Encoding.Default.GetString (Plaindata); returnplaintext; }         Public Statickeyvaluepair<string,string>Creatersakey () {RSACryptoServiceProvider RSA=NewRSACryptoServiceProvider (); stringPrivatekey = RSA. Toxmlstring (true); stringPublicKey = RSA. Toxmlstring (false); return Newkeyvaluepair<string,string>(PublicKey, Privatekey); }         Public Static stringSignData (stringPlainText,stringprivatekeyxml) {RSACryptoServiceProvider provider=NewRSACryptoServiceProvider (); Provider.            Fromxmlstring (Privatekeyxml); byte[] Plaindata =Encoding.Default.GetBytes (plaintext); //set the algorithm to get the digestHashAlgorithm SHA1 = Hashalgorithm.create ("SHA1"); //get a signed digest, which is a digest encrypted with a private key            byte[] Signeddigest =provider.            SignData (Plaindata, SHA1); returnconvert.tobase64string (signeddigest); }         Public Static BOOLVerifydata (stringPlainText,stringSignaturestringpublickeyxml) {RSACryptoServiceProvider provider=NewRSACryptoServiceProvider (); Provider.            Fromxmlstring (Publickeyxml); byte[] Plaindata =Encoding.Default.GetBytes (plaintext); byte[] Signeddigest =convert.frombase64string (signature); HashAlgorithm SHA1= Hashalgorithm.create ("SHA1"); BOOLIsdataintact =provider.            Verifydata (Plaindata, SHA1, signeddigest); returnisdataintact; }        //using Singnhash         Public Static stringSIGNDATA2 (stringPlainText,stringprivatekeyxml) {RSACryptoServiceProvider provider=NewRSACryptoServiceProvider (); Provider.            Fromxmlstring (Privatekeyxml); byte[] Plaindata =Encoding.Default.GetBytes (plaintext); //set the algorithm to get the digestHashAlgorithm SHA1 = Hashalgorithm.create ("SHA1"); //get the original summary            byte[] Digestdata =Sha1.computehash (Plaindata); //to sign an element digest            byte[] Signeddigest = provider. Signhash (Digestdata,"SHA1"); returnconvert.tobase64string (signeddigest); }        //using Verifyhash         Public Static BOOLVERIFYDATA2 (stringPlainText,stringSigneddigest,stringpublickeyxml) {RSACryptoServiceProvider provider=NewRSACryptoServiceProvider (); Provider.            Fromxmlstring (Publickeyxml); byte[] Plaindata =Encoding.Default.GetBytes (plaintext); byte[] Signeddigestdata =convert.frombase64string (signeddigest); //get a local summaryHashAlgorithm SHA1 = Hashalgorithm.create ("SHA1"); byte[] Digest =Sha1.computehash (Plaindata); //decrypting a signature            BOOLIsdataintact = provider. Verifyhash (Digest,"SHA1", Signeddigestdata); returnisdataintact; }    }}

RSACryptoServiceProvider Provider =NewRSACryptoServiceProvider (); stringPublicprivate = provider. Toxmlstring (true);//get a public/private key pair//string publiconly = provider. Toxmlstring (FALSE); //get only the public key//string key = "secret key";            stringstr ="Hello World"; //string str2 = "Hello World"; //Encrypt            stringEncryptedtext =rsacryptohelper.encrypt (publicprivate, str);            Console.WriteLine (Encryptedtext); //learned the refactoring, this is the flexible use of the constructor function.            Cannot directly meet the conditions, it is necessary to find ways to create appropriate. //decryption            stringCleartext =Rsacryptohelper.decrypt (Publicprivate, encryptedtext);            Console.WriteLine (cleartext); stringOriginaldata ="The article is good, this is my signature: Obama! "; Console.WriteLine ("Number of signatures: {0}", Originaldata); KeyValuePair<string,string> KeyPair =Rsacryptohelper.creatersakey (); stringPrivatekey =Keypair.value; stringPublicKey =Keypair.key; //1, generate the signature, through the digest algorithm            stringSigneddata =Rsacryptohelper.signdata (Originaldata, Privatekey); Console.WriteLine ("digital Signature 1:{0}", Signeddata); //2. Verifying Signatures            BOOLVerify =Rsacryptohelper.verifydata (Originaldata, Signeddata, PublicKey); Console.WriteLine ("Signature Verification Result: {0}", verify); stringSIGNEDDATA2 =rsacryptohelper.signdata2 (Originaldata, Privatekey); Console.WriteLine ("digital Signature 2:{0}", SIGNEDDATA2); //2. Verifying Signatures            BOOLVerify2 =rsacryptohelper.verifydata2 (Originaldata, SignedData2, PublicKey); Console.WriteLine ("signature validation result 2:{0}", verify2); 

Reference, extended study:

Data encryption: http://www.cnblogs.com/yank/p/3528548.html

Digital Signature: http://www.cnblogs.com/yank/p/3533998.html

Certificate: http://www.cnblogs.com/Microshaoft/archive/2009/05/19/1460641.html

Encryption: http://www.cnblogs.com/Microshaoft/archive/2008/07/21/1247584.html

Encryption: http://www.cnblogs.com/darrenji/p/3677458.html

(C # Foundation) various cryptographic learning

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.