Detailed. NET encryption and decryption algorithm (3) Asymmetric encryption

Source: Internet
Author: User
Tags bool sha1 asymmetric encryption

This blog post lists the. NET under the commonly used asymmetric encryption algorithm, and they are made into small demo, hope to be helpful to everyone.

Rsa

static string Enrsa (String data,string publickey)  
{  
    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider ();   
    Byte[] cipherbytes;   
    Rsa. Fromxmlstring (PublicKey);   
    Cipherbytes = RSA. Encrypt (Encoding.UTF8.GetBytes (data), false);   
    Return convert.tobase64string (cipherbytes);  
          
}  
      
static string Dersa (String data,string privatekey)  
{  
         
    RSACryptoServiceProvider RSA = new RSACryptoServiceProvider ();   
    Byte[] cipherbytes; Rsa. Fromxmlstring (Privatekey);   
    Cipherbytes = RSA. Decrypt (convert.frombase64string (data), false);   
    Return Encoding.UTF8.GetString (cipherbytes);  
}

Calling code

  Console.WriteLine ("RSA Asymmetric Encryption");  
Byte[] IV = CreateKey ();  
byte[] key = CreateKey ();  
String inputrsa_1 = InputString ();  
RSA RSA = RSA. Create ();  
      
String endata = Enrsa (Inputrsa_1,rsa. Toxmlstring (false));  
      
      
Console.WriteLine ("Encrypted data: {0}", endata);  
Console.WriteLine ("Decrypted data: {0}", Dersa (Endata,rsa). Toxmlstring (true));

DSA (digital signature)

static string Endsa (String data,string publickey)  
  {  
      DSA DSA = DSA. Create ();  
      Byte[] result;  
      Dsa. Fromxmlstring (PublicKey);  
      SHA1 SHA1 = SHA1. Create ();  
      result = DSA. CreateSignature (Sha1.computehash (convert.frombase64string (data));  
      return convert.tobase64string (result);  
     
            
  }  
     
  static bool Dedsa (string data,string privatekey,string originaldata)  
  {  
      //byte[] result;  
      DSA DSA = DSA. Create ();  
      Dsa. Fromxmlstring (Privatekey);  
      SHA1 SHA1 = SHA1. Create ();  
      Return DSA. VerifySignature (Sha1.computehash (convert.frombase64string (Originaldata)), convert.frombase64string (data);  
     
  }

Calling code

  Console.WriteLine ("DSA digital signature");  
String inputdsa_1 = InputString ();  
string inputdsa_2 = inputdsa_1;  
DSA DSA = DSA. Create ();  
String endata = ENDSA (inputdsa_1, DSA. Toxmlstring (True));  
      
Console.WriteLine ("Encrypted data: {0}", endata);  
Console.WriteLine ("Decrypted data: {0}", DEDSA (Endata, DSA). Toxmlstring (False), inputdsa_2));

ECDsa

static string Enecdsa (string data, Cngkey key)  
 {  
     ecdsacng ECDSA = new Ecdsacng (key);  
           
           
     SHA1 SHA1 = SHA1. Create ();  
     Byte[] result;  
      
     result = Ecdsa. Signhash (Sha1.computehash (convert.frombase64string (data));  
      
     return convert.tobase64string (result);  
      
 }  
      
 static bool Deecdsa (string data, Cngkey key,string originaldata)  
 {  
     ecdsacng ECDSA = new Ecdsacng (key);  
     SHA1 SHA1 = SHA1. Create ();  
      
     Return ECDSA. Verifyhash (Sha1.computehash (convert.frombase64string (Originaldata)), convert.frombase64string (data);  
 }

Calling code

  Console.WriteLine ("ECDSA digital signature");  
String inputdsa_1 = InputString ();  
string inputdsa_2 = inputdsa_1;  
Cngkey key = Cngkey.create (cngalgorithm.ecdsap256);  
      
String endata = ENECDSA (inputdsa_1, key);  
      
Console.WriteLine ("Encrypted data: {0}", endata);  
Console.WriteLine ("Decrypted data: {0}", DEECDSA (Endata, Key, inputdsa_2));

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.