C#:一个增强的TcpClient(六)数据加密

using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Splash.Net.Sockets
{   
    /// <summary>
    /// 实现传送数据的加解密
    /// </summary>
    public partial class TcpClientPlus
    {
        /// <summary>
        /// 哈希算法的密钥
        /// </summary>
        private const String _hmackey = "昨夜星辰昨夜风,画楼西畔桂堂东。身无彩凤双飞翼,心有灵犀一点通。";

        /// <summary>
        /// AES对称算法的托管实现
        /// </summary>
        protected AesManaged _aes;

        /// <summary>
        /// 加密密钥字段
        /// </summary>
        private String _secretKey;

        /// <summary>
        /// 加密密钥属性
        /// </summary>
        public String SecretKey
        {
            get { return _secretKey; }
            set
            {
                _secretKey = value;
                if (String.IsNullOrEmpty(value))
                {   // 关闭加密传输模块
                    SecurityClose();  
                }
                else
                {
                    if (_aes == null) _aes = new AesManaged();

                    // 更新加密密钥(256位)
                    using (SHA256Managed sha = new SHA256Managed())
                    {
                        _aes.Key = sha.ComputeHash(Encoding.UTF8.GetBytes(value + _salt));
                        sha.Clear();    // 清除敏感数据
                    }

                    // 更新初始向量(128位)
                    using (HMACMD5 hmacmd5 = new HMACMD5(Encoding.UTF8.GetBytes(_hmackey)))
                    {
                        _aes.IV = hmacmd5.ComputeHash(Encoding.UTF8.GetBytes(_salt));
                        hmacmd5.Clear();    // 清除敏感数据
                    }
                }
            }
        }

        /// <summary>
        /// 最短密码佐料长度
        /// </summary>
        public const Int32 MinSaltLength = 16;

        /// <summary>
        /// 密码佐料字段
        /// </summary>
        private String _salt = "雄关漫道真如铁,而今迈步从头越。从头越,苍山如海,残阳如血。";

        /// <summary>
        /// 密码佐料属性
        /// </summary>
        public String Salt
        {
            get { return _salt; }
            set
            {   // 要求Salt的长度大于16个字符
                if (!String.IsNullOrEmpty(value) && value.Length >= MinSaltLength)
                {
                    _salt = value;
                    if (!String.IsNullOrEmpty(_secretKey))
                    {   // 更新加密密钥(256位)
                        using (SHA256Managed sha = new SHA256Managed())
                        {
                            _aes.Key = sha.ComputeHash(Encoding.UTF8.GetBytes(_secretKey + value));
                            sha.Clear();    // 清除敏感数据
                        }

                        // 更新初始向量(128位)
                        using (HMACMD5 hmacmd5 = new HMACMD5(Encoding.UTF8.GetBytes(_hmackey)))
                        {
                            _aes.IV = hmacmd5.ComputeHash(Encoding.UTF8.GetBytes(value));
                            hmacmd5.Clear();    // 清除敏感数据
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 加密数据
        /// </summary>
        /// <param name="buffer">原始数据</param>
        /// <param name="offset">字节偏移量</param>
        /// <param name="count">要写入当前流的字节数</param>
        /// <returns>加密后的数据</returns>        
        private Byte[] Encrypt(Byte[] buffer, Int32 offset, Int32 count)
        {
            using(MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, _aes.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(buffer, offset, count);
                    cs.Close();
                    return ms.ToArray();
                }
            }
        }

        /// <summary>
        /// 解密数据
        /// </summary>
        /// <param name="buffer">原始数据</param>
        /// <param name="offset">字节偏移量</param>
        /// <param name="count">要写入当前流的字节数</param>
        /// <returns>解密后的数据</returns>
        private Byte[] Decrypt(Byte[] buffer, Int32 offset, Int32 count)
        {
            using (MemoryStream ms = new MemoryStream())
            {
                using (CryptoStream cs = new CryptoStream(ms, _aes.CreateDecryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(buffer, offset, count);
                    cs.Close();
                    return ms.ToArray();
                }
            }
        }

        /// <summary>
        /// 关闭加密传输模块
        /// </summary>
        private void SecurityClose()
        {
            if (_aes != null)
            {
                _aes.Clear();    // 清除敏感数据
                _aes.Dispose();  // 释放资源
                _aes = null;
            }
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值