• 对称加密解密


    /// <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);
            }
        }
    
  • 相关阅读:
    Google Code 项目代码托管网站上 Git 版本控制系统使用简明教程
    C/C++预定义宏
    使用 Raspberry Pi 远程桌面
    Vim 中将 tab 自动转换成空格
    DR模式搭建LVS负载均衡
    NAT模式LVS搭建负载均衡集群
    php扩展memached安装
    raw_input与input的区别
    keepalived+lvs搭建高可用负载均衡集群
    使用keepalived搭建nginx高可用
  • 原文地址:https://www.cnblogs.com/jysun/p/3934811.html
Copyright © 2020-2023  润新知