• 对称加密解密


    /// <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);
            }
        }
    
  • 相关阅读:
    .net framework 3.5 和 4.0 的结构图以及Namespaces参考,强烈推荐下载了解!
    ASP.NET操作简单的xml,增删改查
    Http协议详解版本一
    asp.net ToString()格式汇总
    UC首页图片切换
    vs2005部署错误解决方法:ASPNETMERGE : error 1013: Cannot find any assemblies that can be merged in the application bin folder.
    ADO.net,Linq to SQL和Entity Framework性能实测分析
    vs2010下载地址
    如何使用iReaper来下载微软视频教程
    面试经典70题
  • 原文地址:https://www.cnblogs.com/jysun/p/3934811.html
Copyright © 2020-2023  润新知