C#簡單的加密類

來源:互聯網
上載者:User
public class EncryptHepler {
        // 驗值 
        static string saltValue = "XXXX";
        // 密碼值 
        static string pwdValue = "XXXX";

        /// <summary>
        /// 加密
        /// </summary>
        public static string Encrypt( string input ) {
            byte[ ] data = System.Text.UTF8Encoding.UTF8.GetBytes( input );
            byte[ ] salt = System.Text.UTF8Encoding.UTF8.GetBytes( saltValue );

            // AesManaged - 進階加密標準(AES) 對稱演算法的管理類 
            System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged( );
            // Rfc2898DeriveBytes - 通過使用基於 HMACSHA1 的偽隨機數產生器,實現基於密碼的金鑰衍生函數 (Key Derivation Function) (PBKDF2 - 一種基於密碼的密鑰派生函數) 
            // 通過 密碼 和 salt 衍生金鑰 
            System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes( pwdValue, salt );

            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            aes.Key = rfc.GetBytes( aes.KeySize / 8 );
            aes.IV = rfc.GetBytes( aes.BlockSize / 8 );

            // 用當前的 Key 屬性和初始化向量 IV 建立對稱式加密器對象 
            System.Security.Cryptography.ICryptoTransform encryptTransform = aes.CreateEncryptor( );
            // 加密後的輸出資料流 
            System.IO.MemoryStream encryptStream = new System.IO.MemoryStream( );
            // 將加密後的目標流(encryptStream)與加密轉換(encryptTransform)相串連 
            System.Security.Cryptography.CryptoStream encryptor = new System.Security.Cryptography.CryptoStream
                ( encryptStream, encryptTransform, System.Security.Cryptography.CryptoStreamMode.Write );

            // 將一個位元組序列寫入當前 CryptoStream (完成加密的過程)
            encryptor.Write( data, 0, data.Length );
            encryptor.Close( );
            // 將加密後所得到的流轉換成位元組數組,再用Base64編碼將其轉換為字串 
            string encryptedString = Convert.ToBase64String( encryptStream.ToArray( ) );
            return encryptedString;
        }
        /// <summary>
        /// 解密
        /// </summary>
        public static string Decrypt( string input ) {
            byte[ ] encryptBytes = Convert.FromBase64String( input );
            byte[ ] salt = Encoding.UTF8.GetBytes( saltValue );
            System.Security.Cryptography.AesManaged aes = new System.Security.Cryptography.AesManaged( );
            System.Security.Cryptography.Rfc2898DeriveBytes rfc = new System.Security.Cryptography.Rfc2898DeriveBytes( pwdValue, salt );

            aes.BlockSize = aes.LegalBlockSizes[0].MaxSize;
            aes.KeySize = aes.LegalKeySizes[0].MaxSize;
            aes.Key = rfc.GetBytes( aes.KeySize / 8 );
            aes.IV = rfc.GetBytes( aes.BlockSize / 8 );

            // 用當前的 Key 屬性和初始化向量 IV 建立對稱解密器對象 
            System.Security.Cryptography.ICryptoTransform decryptTransform = aes.CreateDecryptor( );
            // 解密後的輸出資料流 
            System.IO.MemoryStream decryptStream = new System.IO.MemoryStream( );

            // 將解密後的目標流(decryptStream)與解密轉換(decryptTransform)相串連 
            System.Security.Cryptography.CryptoStream decryptor = new System.Security.Cryptography.CryptoStream(
                decryptStream, decryptTransform, System.Security.Cryptography.CryptoStreamMode.Write );
            // 將一個位元組序列寫入當前 CryptoStream (完成解密的過程) 
            decryptor.Write( encryptBytes, 0, encryptBytes.Length );
            decryptor.Close( );

            // 將解密後所得到的流轉換為字串 
            byte[ ] decryptBytes = decryptStream.ToArray( );
            string decryptedString = UTF8Encoding.UTF8.GetString( decryptBytes, 0, decryptBytes.Length );
            return decryptedString;
        }
    }//class end

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.