Detailed. NET encryption and decryption algorithm (2) symmetric encryption

Source: Internet
Author: User
Tags flush rand readline

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

Common code

Static byte[] CreateKey (int num)  
{  
    byte[] result = new Byte[num];  
    Random rand = new Random ();  
    for (int i = 0; i < num i++)  
    {  
        Result[i] = (Byte) rand. Next (1, 256);  
    }  
    return result;  
      
}

Des

&lt;summary&gt;///des encryption algorithm must use Base64 byte object///&lt;/summary&gt;///&lt;param name= "Data" &gt; character data to be encrypted &lt;/par am&gt;///&lt;param name= "key" &gt; key, length must be 64 bits (byte[8)) &lt;/param&gt;///&lt;param name= "IV" &gt;iv vector, must be 64 bits in length (byte  
[8]) &lt;/param&gt;///&lt;returns&gt; encrypted character &lt;/returns&gt; static string endes (string data, byte[] key, byte[] IV) {des des = des.  
    Create ();  
    This line of code is important and requires different conversion formats to be selected according to different strings byte[] tmp = Encoding.Unicode.GetBytes (data);  
      
    Byte[] Encryptodata; ICryptoTransform encryptor = des.  
    CreateEncryptor (key, iv); using (MemoryStream MemoryStream = new MemoryStream ()) {using (var cs = new CryptoStream (MemoryStream, en  
            Cryptor, CryptoStreamMode.Write)) {using (StreamWriter writer = new StreamWriter (CS)) {writer.  
                Write (data); Writer.  
            Flush ();  
    } Encryptodata = Memorystream.toarray ();  
  }  Des.  
      
    Clear ();  
      
Return convert.tobase64string (Encryptodata);  
///&lt;summary&gt;///des decryption algorithm///&lt;/summary&gt;///&lt;param name= "Data" &gt; Encrypted character data &lt;/param&gt; &lt;param name= "key" &gt; key, length must be 64 bits (byte[8)) &lt;/param&gt;///&lt;param name= "IV" &GT;IV vector, length must be 64 bits (BYTE[8))  
    &lt;/param&gt;///&lt;returns&gt; Encrypted character &lt;/returns&gt; static string Dedes (string data, byte[] key, byte[] IV { String resultdata = String.  
    Empty; This line of code is important byte[] Tmpdata = convert.frombase64string (data);//The format of the conversion is very important des des = des.  
      
    Create (); ICryptoTransform decryptor = des.  
    CreateDecryptor (key, iv); using (var MemoryStream = new MemoryStream (tmpdata)) {using (var cs = new CryptoStream (MemoryStream, DECR  
            Yptor, CryptoStreamMode.Read)) {StreamReader reader = new StreamReader (CS); Resultdata = reader.  
        ReadLine (); } return resultData; }

Calling code

Des symmetric encryption  
Console.WriteLine ("Des symmetric Encryption");  
Byte[] IV = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};  
byte[] key = new byte[] {1, 2, 3, 4, 5, 6, 7, 8};  
String inputdes_1 = InputString ();  
String endata = Endes (Inputdes_1, Key, iv);<p>  
Console.WriteLine ("Encrypted data: {0}", endata);  
Console.WriteLine ("Decrypted data: {0}", Dedes (Endata, key, iv));</p>

TripleDES (3DES)

///<summary>///tripledes encryption///</summary>///<param name= "Data" > character data to be encrypted </param> <param name= "key" > key, Length can be: 128-bit (byte[16]), 192-bit (byte[24)) </param>///<param name= "IV" >iv vector, Length must be 64-bit (byte[8]) </param>///<returns> Encrypted characters </returns> static string Entripledes (string data, byte  
    [] key, byte[] IV {byte[] tmp = NULL;  
     
    byte[] Tmpdata = Encoding.Unicode.GetBytes (data);  
     
     
    TripleDES TripleDES = Tripledes.create ();  
    ICryptoTransform encryptor = Tripledes.createencryptor (key, iv); using (MemoryStream ms = new MemoryStream ()) {using (CryptoStream cs = new CryptoStream (MS, Encryptor, Cr  
            Yptostreammode.write)) {StreamWriter writer = new StreamWriter (CS); Writer.  
            Write (data); Writer. Flush ()//This sentence is very important, after the convection operation must use this sentence to force all data in the buffer to be written to the target object} tmp = Ms.  
    ToArray (); Return convert.toBase64string (TMP); ///<summary>///tripledes decryption///</summary>///<param name= "Data" > character data to be encrypted </PARAM&G  
T <param name= "key" > key, Length can be: 128-bit (byte[16]), 192-bit (byte[24)) </param>///<param name= "IV" >iv vector, Length must be 64-bit (byte[8]) </param>///<returns> Encrypted characters </returns> static string Detripledes (string data, byte  
    [] key, byte[] IV {byte[] tmp = convert.frombase64string (data); string result = String.  
     
    Empty;  
    TripleDES TripleDES = Tripledes.create ();  
     
    ICryptoTransform decryptor = Tripledes.createdecryptor (key, iv);  using (MemoryStream ms = new MemoryStream (tmp)) {using (CryptoStream cs = new CryptoStream (MS, Decryptor,  
            CryptoStreamMode.Read)) {StreamReader reader = new StreamReader (CS); result = reader.  
        ReadLine ();  
    } tripledes.clear ();  

return result; }

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.