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 ();