使用DES算法實現加密解密
我們常見的加密算法有DES、MD5、IDEA、AES等等,這篇隨筆介紹使用DES算法實現加密解密
首先介紹一下DES算法:
DES算法為密碼體制中的對稱密碼體制,又被稱為美國數據加密標准,是1972年美國IBM公司研制的對稱密碼體制加密算法。 明文按64位進行分組,密鑰長64位,密鑰事實上是56位參與DES運算(第8、16、24、32、40、48、56、64位是校驗位, 使得每個密鑰都有奇數個1)分組后的明文組和56位的密鑰按位替代或交換的方法形成密文組的加密方法。
DES算法基本原理:
其入口參數有三個:key、data、mode。key為加密解密使用的密鑰,data為加密解密的數據,mode為其工作模式。當模式為加密模式時,明文按照64位進行分組,形成明文組,key用於對數據加密,當模式為解密模式時,key用於對數據解密。實際運用中,密鑰只用到了64位中的56位,這樣才具有高的安全性。
附上常用的DES算法加密解密類:
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 /// 密匙 30 /// </summary> 31 public static string Key 32 { 33 get 34 { 35 return "hongye10"; 36 } 37 } 38 /// <summary> 39 /// 加密字符串 40 /// 注意:密鑰必須為8位 41 /// </summary> 42 /// <param name="strText">字符串</param> 43 /// <param name="encryptKey">密鑰</param> 44 /// <param name="encryptKey">返回加密后的字符串</param> 45 public static string DesEncrypt(string inputString, string encryptKey) 46 { 47 byte[] byKey = null; 48 byte[] IV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 49 try 50 { 51 byKey = System.Text.Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8)); 52 DESCryptoServiceProvider des = new DESCryptoServiceProvider();//加密服務提供者類 53 byte[] inputByteArray = Encoding.UTF8.GetBytes(inputString); 54 MemoryStream ms = new MemoryStream();//內存流 55 //將數據流連接到加密轉換的流 56 CryptoStream cs = 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">加了密的字符串</param> 71 /// <param name="decryptKey">密鑰</param> 72 /// <param name="decryptKey">返回解密后的字符串</param> 73 public static string DesDecrypt(string 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 }
End!