1 /// <summary> 2 /// DES加密(数据加密标准,速度较快,适用于加密大量数据的场合) 3 /// </summary> 4 /// <param name="EncryptString">待加密的密文</param> 5 /// <param name="EncryptKey">加密的密钥</param> 6 /// <returns>returns</returns> 7 public static string DESEncrypt(string EncryptString, string EncryptKey) 8 { 9 if (string.IsNullOrEmpty(EncryptString)) 10 { 11 EncryptString = ConfigurationHelper.GetArasDatabaseName(); 12 } 13 14 if (string.IsNullOrEmpty(EncryptKey) || EncryptKey.Length != 8) 15 { 16 EncryptKey = "ArasInnovator"; 17 } 18 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 19 string m_strEncrypt = ""; 20 DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider(); 21 22 try 23 { 24 byte[] m_btEncryptString = Encoding.Default.GetBytes(EncryptString); 25 MemoryStream m_stream = new MemoryStream(); 26 CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateEncryptor(Encoding.Default.GetBytes(EncryptKey), m_btIV), CryptoStreamMode.Write); 27 m_cstream.Write(m_btEncryptString, 0, m_btEncryptString.Length); 28 m_cstream.FlushFinalBlock(); 29 m_strEncrypt = Convert.ToBase64String(m_stream.ToArray()); 30 m_stream.Close(); m_stream.Dispose(); 31 m_cstream.Close(); m_cstream.Dispose(); 32 } 33 catch (IOException ex) { throw ex; } 34 catch (CryptographicException ex) { throw ex; } 35 catch (ArgumentException ex) { throw ex; } 36 catch (Exception ex) { throw ex; } 37 finally { m_DESProvider.Clear(); } 38 39 return m_strEncrypt; 40 }
1 /// <summary> 2 /// DES 解密(数据加密标准,速度较快,适用于加密大量数据的场合) 3 /// </summary> 4 /// <param name="DecryptString">待解密的密文</param> 5 /// <param name="DecryptKey">解密的密钥</param> 6 /// <returns>returns</returns> 7 public static string DESDecrypt(string DecryptString, string DecryptKey) 8 { 9 if (string.IsNullOrEmpty(DecryptString)) 10 { 11 DecryptString = ConfigurationHelper.GetArasDatabaseName(); 12 } 13 14 if (string.IsNullOrEmpty(DecryptKey) || DecryptKey.Length != 8) 15 { 16 DecryptKey = "ArasInnovator"; 17 } 18 19 byte[] m_btIV = { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF }; 20 string m_strDecrypt = ""; 21 DESCryptoServiceProvider m_DESProvider = new DESCryptoServiceProvider(); 22 23 try 24 { 25 byte[] m_btDecryptString = Convert.FromBase64String(DecryptString); 26 MemoryStream m_stream = new MemoryStream(); 27 CryptoStream m_cstream = new CryptoStream(m_stream, m_DESProvider.CreateDecryptor(Encoding.Default.GetBytes(DecryptKey), m_btIV), CryptoStreamMode.Write); 28 m_cstream.Write(m_btDecryptString, 0, m_btDecryptString.Length); 29 m_cstream.FlushFinalBlock(); 30 m_strDecrypt = Encoding.Default.GetString(m_stream.ToArray()); 31 m_stream.Close(); m_stream.Dispose(); 32 m_cstream.Close(); m_cstream.Dispose(); 33 } 34 35 catch (IOException ex) { } 36 catch (CryptographicException ex) { } 37 catch (ArgumentException ex) { } 38 catch (Exception ex) { } 39 finally { m_DESProvider.Clear(); } 40 41 return m_strDecrypt; 42 }
【原文出处】http://www.51aras.com/?id=8