Use DES encryption and decryption algorithm

Use DES encryption and decryption algorithm

The most common encryption algorithms DES, MD5, IDEA, AES, etc., this essay describes the use of DES encryption and decryption algorithm

First, tell us about the DES algorithm:

DES algorithm is a symmetric cryptosystem cryptosystem, and is also known as US Data Encryption Standard, an encryption algorithm is a symmetric cryptosystem United States in 1972 developed by IBM. Plain text by grouping 64, 64-bit key length, the key 56 is in fact involved in the DES operation (8,16,24,32,40,48,56,64 first bit is the parity bit, such that each ciphertext has an odd number key 1) plaintexts and 56-bit key encryption method after the packet is formed by the method set ciphertext bits or alternatively exchanged.

DES algorithm basic principles:

Its inlet there are three parameters: key, data, mode. Key for the encryption and decryption key used, data encryption and decryption of data, mode to its working mode. When the mode encryption mode, according to the 64-bit plaintext packets, a plaintext form groups, key used to encrypt the data, when the mode is in decryption mode, key used to decrypt the data. Practice, only use a 64-bit key in 56, so that it has a high safety.

Attached commonly used DES encryption and decryption algorithm categories:

 1 using System;
 2 using System.IO;
 3 using System.Security.Cryptography;
 4 using System.Text;
 5 
 6 namespace CodeFirst.Common
 7 {
 8     public class Encryption
 9     {
10         /// <summary>
11         /// 加密
12         /// </summary>
13         /// <param name="inputString">加密的字符串</param>
14         /// <returns></returns>
15         public static string DesEncrypt(string inputString)
16         {
17             return DesEncrypt(inputString, Key);
18         }
19         /// <summary>
20         /// 解密
21         /// </summary>
22         /// <param name="inputString">解密的字符串</param>
23         /// <returns></returns>
24         public static string DesDecrypt(string inputString)
25         {
26             return DesDecrypt(inputString, Key);
27         }
 28          ///  <Summary> 
29          /// key
 30          ///  </ Summary> 
31 is          public  static  String Key
 32          {
 33 is              GET 
34 is              {
 35                  return  " hongye10 " ;
 36              }
 37 [          }
 38 is          ///  <Summary> 
39          /// encrypted string
 40          /// Note: The key must be 8 bits
 41 is          ///  </ Summary> 
42 is          ///  <param name = "strText"> string</ param> 
43 is          ///  <param name = "encryptkey"> Key </ param> 
44 is          ///  <param name = "encryptkey"> return encrypted string </ param> 
45          public  static  String DesEncrypt ( String the inputString, String encryptkey)
 46 is          {
 47              byte [] byKey = null ;
 48              byte [] IV = { 0x12 , 0x34 , 0x56 , 0x78 , 0x90 , 0xAB , 0xCD , 0xEF};
 49              the try 
50              {
 51 is                  byKey = System.Text.Encoding.UTF8.GetBytes (encryptKey.Substring ( 0 , . 8 ));
 52 is                  the DESCryptoServiceProvider des = new new the DESCryptoServiceProvider (); // cryptographic service provider class 
53 is                  byte [] The inputByteArray = Encoding.UTF8.GetBytes (the inputString);
 54 is                  the MemoryStream MS = new new the MemoryStream (); // memory stream
 55                  // the encrypted data stream to the stream converter 
56 is                  the CryptoStream CS = new new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
57                 cs.Write(inputByteArray, 0, inputByteArray.Length);
58                 cs.FlushFinalBlock();
59                 return Convert.ToBase64String(ms.ToArray());
60             }
61             catch (System.Exception error)
62             {
63                 //return error.Message;
64                 return null;
65             }
66         }
67         /// <summary>
68         /// 解密字符串
69         ///  </ Summary> 
70          ///  <param name = "this.inputString"> added string encrypted </ param> 
71 is          ///  <param name = "decryptKey"> Key </ param> 
72          ///  <param name = "decryptKey"> return the decrypted string </ param> 
73 is          public  static  string DesDecrypt ( string the inputString, string decryptKey)
 74          {
 75              byte [] byKey = null ;
 76              byte [] = {IV 0x12 , 0x34 , 0x56 ,0x78, 0x90, 0xAB, 0xCD, 0xEF };
77             byte[] inputByteArray = new Byte[inputString.Length];
78             try
79             {
80                 byKey = System.Text.Encoding.UTF8.GetBytes(decryptKey.Substring(0, 8));
81                 DESCryptoServiceProvider des = new DESCryptoServiceProvider();
82                 inputByteArray = Convert.FromBase64String(inputString);
83                 MemoryStream ms = new MemoryStream();
84                 CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
85                 cs.Write(inputByteArray, 0, inputByteArray.Length);
86                 cs.FlushFinalBlock();
87                 System.Text.Encoding encoding = new System.Text.UTF8Encoding();
88                 return encoding.GetString(ms.ToArray());
89             }
90             catch (System.Exception error)
91             {
92                 //return error.Message;
93                 return null;
94             }
95         }
96     }
97 }

 

Guess you like

Origin www.cnblogs.com/gygg/p/11274696.html