AesEncryptHelper帮助类,实现C#版Aes加解密

 /// 
    /// 
    /// 
    internal class AesEncryptHelper
    {
        /// 
        /// Encrypts the specified asc key.
        /// 
        /// The asc key.
        /// To encrypt.
        /// 
        public static string Encrypt(string ascKey, string toEncrypt)
        {
            byte[] keyArray = Encoding.UTF8.GetBytes(ascKey);
            byte[] toEncryptArray = Encoding.UTF8.GetBytes(toEncrypt);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateEncryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Convert.ToBase64String(resultArray, 0, resultArray.Length);
        }

        /// 
        /// Decrypts the specified asc key.
        /// 
        /// The asc key.
        /// To decrypt.
        /// 
        public static string Decrypt(string ascKey, string toDecrypt)
        {
            byte[] keyArray = Encoding.UTF8.GetBytes(ascKey);
            byte[] toEncryptArray = Convert.FromBase64String(toDecrypt);
            RijndaelManaged rDel = new RijndaelManaged();
            rDel.Key = keyArray;
            rDel.Mode = CipherMode.ECB;
            rDel.Padding = PaddingMode.PKCS7;
            ICryptoTransform cTransform = rDel.CreateDecryptor();
            byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
            return Encoding.UTF8.GetString(resultArray);
        }
    }

 

你可能感兴趣的:(技巧共享,C#常用工具类与开发技巧,C#,Aes,加密,解密)