对称加密解密

/// <summary>
    /// 帮助类
    /// </summary>
    public class CryptoHelper
    {
        //// 对称加密算法提供器

        /// <summary>
        /// 加密器对象
        /// </summary>
        private readonly ICryptoTransform encryptor;

        /// <summary>
        /// 解密器对象
        /// </summary>
        private readonly ICryptoTransform decryptor;

        /// <summary>
        /// 缓存
        /// </summary>
        private const int BufferSize = 1024;

        /// <summary>
        /// Initializes a new instance of the <see cref="CryptoHelper"/> class.
        /// </summary>
        /// <param name="algorithmName">
        /// algorithmName
        /// </param>
        /// <param name="key">
        ///  密钥
        /// </param>
        public CryptoHelper(string algorithmName, string key)
        {
            SymmetricAlgorithm _provider = SymmetricAlgorithm.Create(algorithmName);
            _provider.Key = Encoding.UTF8.GetBytes(key);
            _provider.IV = new byte[] { 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };

            encryptor = _provider.CreateEncryptor();
            decryptor = _provider.CreateDecryptor();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="CryptoHelper"/> class.
        /// </summary>
        /// <param name="key">
        /// 密钥
        /// </param>
        public CryptoHelper(string key) : this("TripleDES", key) { }

        /// <summary>
        /// 加密算法
        /// </summary>
        /// <param name="clearText">
        /// The clear text.
        /// </param>
        /// <returns>
        /// 密文
        /// </returns>
        public string Encrypt(string clearText)
        {
            //// 创建明文流
            byte[] _clearBuffer = Encoding.UTF8.GetBytes(clearText);
            var _clearStream = new MemoryStream(_clearBuffer);

            //// 创建空的密文流
            var _encryptedStream = new MemoryStream();

            var _cryptoStream = new CryptoStream(_encryptedStream, encryptor, CryptoStreamMode.Write);

            //// 将明文流写入到buffer中
            //// 将buffer中的数据写入到cryptoStream中
            int _bytesRead;
            var _buffer = new byte[BufferSize];
            do
            {
                _bytesRead = _clearStream.Read(_buffer, 0, BufferSize);
                _cryptoStream.Write(_buffer, 0, _bytesRead);
            } 
            while (_bytesRead > 0);

            _cryptoStream.FlushFinalBlock();

            //// 获取加密后的文本
            _buffer = _encryptedStream.ToArray();
            string _encryptedText = Convert.ToBase64String(_buffer);
            return _encryptedText;
        }
        
        /// <summary>
        ///  解密算法
        /// </summary>
        /// <param name="encryptedText">
        /// The encrypted text.
        /// </param>
        /// <returns>
        /// 解密字符串
        /// </returns>
        public string Decrypt(string encryptedText)
        {
            byte[] _encryptedBuffer = Convert.FromBase64String(encryptedText);
            Stream _encryptedStream = new MemoryStream(_encryptedBuffer);

            var _clearStream = new MemoryStream();
            var _cryptoStream = new CryptoStream(_encryptedStream, decryptor, CryptoStreamMode.Read);

            int _bytesRead;
            var _buffer = new byte[BufferSize];

            do
            {
                _bytesRead = _cryptoStream.Read(_buffer, 0, BufferSize);
                _clearStream.Write(_buffer, 0, _bytesRead);
            }
            while (_bytesRead > 0);

            _buffer = _clearStream.GetBuffer();
            string _clearText = Encoding.UTF8.GetString(_buffer, 0, (int) _clearStream.Length);

            return _clearText;
        }

        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="clearText">
        /// 连接字符串
        /// </param>
        /// <param name="key">
        /// 密钥
        /// </param>
        /// <returns>
        /// 加密后的字符串
        /// </returns>
        public static string Encrypt(string clearText, string key)
        {
            var _helper = new CryptoHelper(key);
            return _helper.Encrypt(clearText);
        }

        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="encryptedText">
        /// 连接字符串
        /// </param>
        /// <param name="key">
        /// 密钥
        /// </param>
        /// <returns>
        /// 解密后的字符串
        /// </returns>
        public static string Decrypt(string encryptedText, string key)
        {
            var _helper = new CryptoHelper(key);
            return _helper.Decrypt(encryptedText);
        }
    }
原文地址:https://www.cnblogs.com/jysun/p/3934811.html