Detailed. NET encryption and decryption algorithm (6) play the Asymmetric encryption

Source: Internet
Author: User
Tags decrypt asymmetric encryption

Ben Bovinlai chat How to play asymmetric encryption bar, here is mainly introduced. NET algorithm, 3 kinds of asymmetric encryption algorithms: DSA,RSA,ECDSA. The two blog posts have found the lead for the hash family and the asymmetric encryption family, so let's find the right lead for asymmetric cryptography.

First create an interface: "Iencryptanddecrypt", and then create 3 implementation classes for the above 3 algorithm and let these classes implement the interface "Iencryptanddecrypt". They are in the following picture:

Now that we've got these guys together, we're going to find them a lead: "Encryptanddecryptinvoker." We'll be able to contact the lead directly when we're looking for these asymmetric encryption family buddies. Let's see the team now:

It looks pretty good, let's see if our lead is up to its work:

Encryptanddecryptinvoker lead;  

            String data = "We are asymmetric encryption family!";  
            Execution Environment cryptogramsetting setting = new cryptogramsetting ();  
            Byte[] bdata;//after the encryption processing byte[] adata;//confidential processing byte[] TMP;  
            Encoding Encoding = System.Text.Encoding.UTF8; Sys_cryptography. DSA TMP_DSA = sys_cryptography. Dsa.  
            Create (); String Dsa_publickey = Tmp_dsa.  
            Toxmlstring (FALSE); String Dsa_privatekey = Tmp_dsa.  
            Toxmlstring (TRUE); Sys_cryptography. RSA Tmp_rsa = sys_cryptography. Rsa.  
            Create (); String Rsa_publickey = Tmp_rsa.  
            Toxmlstring (FALSE); String Rsa_privatekey = Tmp_rsa.  
            Toxmlstring (TRUE); Sys_cryptography. ECDsa TMP_ECDSA = sys_cryptography.  
            Ecdsa.create (); String Ecdsa_publickey = Tmp_ecdsa.  
            Toxmlstring (FALSE); String Ecdsa_privatekey = Tmp_ecdsa.  

            Toxmlstring (TRUE);  
                  
     Dsa       Hashtable Dsakey = new Hashtable ();  
            dsakey["PublicKey"] = Dsa_publickey;  
            dsakey["Privatekey"] = Dsa_privatekey; Setting.  
            Cryptogramtype = Cryptogramtype.unsymmetryencryptanddecrypt; Setting.  
      
      
            Publickeyandprivatekey = Dsakey;  
                  
            Lead = new Encryptanddecryptinvoker (new DSA (), setting);  
            Console.WriteLine ("DSA:"); TMP = lead. Encrypt (encoding.  
            GetBytes (data));  
      
            Console.WriteLine ("Signature:" +convert.tobase64string (TMP)); Lead. Setvertifydata (encoding. GetBytes (data))//verify signature bool isoriginal = lead. Decrypt (TMP) [0] = = 1?  
      
            True:false;  

            Console.WriteLine ("Signature Result:" + isoriginal);  
            RSA Hashtable Rsakey = new Hashtable ();  
            rsakey["PublicKey"] = Rsa_publickey;  
            rsakey["Privatekey"] = Rsa_privatekey; Setting. Cryptogramtype = cryptogramtyPe.  
            Unsymmetryencryptanddecrypt; Setting.  
      
            Publickeyandprivatekey = Rsakey;  
            Lead = new Encryptanddecryptinvoker (new RSA (), setting);  
            Console.WriteLine ("RSA:"); TMP = lead. Encrypt (encoding.  
            GetBytes (data));  
            Console.WriteLine ("Encryption:" + convert.tobase64string (TMP)); TMP = lead.  
            Decrypt (TMP); Console.WriteLine ("Decrypt:" + encoding.)  
      
      
            GetString (TMP));  
            ECDsa problem: ECDsa can not be everywhere public key and private key Hashtable Ecdsakey = new Hashtable ();  
            ecdsakey["PublicKey"] = Ecdsa_publickey;  
            ecdsakey["Privatekey"] = Ecdsa_privatekey; Setting.  
            Cryptogramtype = Cryptogramtype.unsymmetryencryptanddecrypt; Setting.  
      
            Publickeyandprivatekey = Ecdsakey;  
            Lead = new Encryptanddecryptinvoker (new ECDsa (), setting);  
            Console.WriteLine ("ECDsa:"); TMP = lead. Encrypt (encoding. GetBytes (dATA));  
            Console.WriteLine ("Encryption:" + convert.tobase64string (TMP)); TMP = lead.  
            Decrypt (TMP); Console.WriteLine ("Decrypt:" + encoding.)  

            GetString (TMP)); Console.readkey ();

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.