DES encryption / decryption

. 1  ///  <Summary> 
2          /// the DES encryption (Data Encryption Standard, fast, suitable for large collections of data encryption)
 . 3          ///  </ Summary> 
. 4          ///  <param name = "encryptString"> be a ciphertext encrypted </ param> 
. 5          ///  <param name = "encryptkey"> encryption key </ 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          /// the DES decryption (data encryption standard, fast, suitable for large collections of data encryption)
 . 3          ///  </ Summary> 
. 4          ///  <param name = "DecryptString"> be decrypted ciphertext </ param> 
. 5          ///  <param name = "decryptKey"> key to decrypt </ 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         }

 

Guess you like

Origin www.cnblogs.com/61007257Steven/p/10953410.html