標籤:style blog http color 使用 os io for
1.加密
1 public class EncryptHepler { 2 // 驗值 3 static string saltValue = "XXXX"; 4 // 密碼值 5 static string pwdValue = "XXXX"; 6 7 /// <summary> 8 /// 加密 9 /// </summary>10 public static string Encrypt( string input ) {11 byte[ ] data = System.Text.UTF8Encoding.UTF8.GetBytes( input );12 byte[ ] salt = System.Text.UTF8Encoding.UTF8.GetBytes( saltValue );13 14 // AesManaged - 進階加密標準(AES) 對稱演算法的管理類 15 System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged( );16 // Rfc2898DeriveBytes - 通過使用基於 HMACSHA1 的偽隨機數產生器,實現基於密碼的金鑰衍生函數 (Key Derivation Function) (PBKDF2 - 一種基於密碼的密鑰派生函數) 17 // 通過 密碼 和 salt 衍生金鑰 18 System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes( pwdValue, salt );19 20 aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;21 aes.KeySize = aes.LegalKeySizes[0].MaxSize;22 aes.Key = rfc.GetBytes( aes.KeySize / 8 );23 aes.IV = rfc.GetBytes( aes.BlockSize / 8 );24 25 // 用當前的 Key 屬性和初始化向量 IV 建立對稱式加密器對象 26 System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor( );27 // 加密後的輸出資料流 28 System.IO.MemoryStream encryptStream = new System.IO.MemoryStream( );29 // 將加密後的目標流(encryptStream)與加密轉換(encryptTransform)相串連 30 System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream31 ( encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write );32 33 // 將一個位元組序列寫入當前 CryptoStream (完成加密的過程)34 encryptor.Write( data, 0, data.Length );35 encryptor.Close( );36 // 將加密後所得到的流轉換成位元組數組,再用Base64編碼將其轉換為字串 37 string encryptedString = Convert.ToBase64String( encryptStream.ToArray( ) );38 return encryptedString;39 }
View Code
2.解密
1 /// <summary> 2 /// 解密 3 /// </summary> 4 public static string Decrypt( string input ) { 5 byte[ ] encryptBytes = Convert.FromBase64String( input ); 6 byte[ ] salt = Encoding.UTF8.GetBytes( saltValue ); 7 System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged( ); 8 System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes( pwdValue, salt ); 9 10 aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;11 aes.KeySize = aes.LegalKeySizes[0].MaxSize;12 aes.Key = rfc.GetBytes( aes.KeySize / 8 );13 aes.IV = rfc.GetBytes( aes.BlockSize / 8 );14 15 // 用當前的 Key 屬性和初始化向量 IV 建立對稱解密器對象 16 System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor( );17 // 解密後的輸出資料流 18 System.IO.MemoryStream decryptStream = new System.IO.MemoryStream( );19 20 // 將解密後的目標流(decryptStream)與解密轉換(decryptTransform)相串連 21 System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(22 decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write );23 // 將一個位元組序列寫入當前 CryptoStream (完成解密的過程) 24 decryptor.Write( encryptBytes, 0, encryptBytes.Length );25 decryptor.Close( );26 27 // 將解密後所得到的流轉換為字串 28 byte[ ] decryptBytes = decryptStream.ToArray( );29 string decryptedString = UTF8Encoding.UTF8.GetString( decryptBytes, 0, decryptBytes.Length );30 return decryptedString;31 }32 }//class end
View Code