C#:一个增强的TcpClient(六)数据加密
2012-05-09 15:07
726 查看
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; } } } }
相关文章推荐
- C#:一个增强的TcpClient(*)客户端演示程序及源代码下载
- C#:一个增强的TcpClient(四)查询
- C#:一个增强的TcpClient(五)构造函数
- C#:一个增强的TcpClient(三)读取
- C#TCPClient应用-一个简单的消息发送和接收 (转)
- C#:一个增强的TcpListener(*)服务器演示程序及源代码下载
- C#TCPClient应用-一个简单的消息发送和接收
- C#:一个增强的TcpListener(*)服务器演示程序及源代码下载
- C#TCPClient应用-一个简单的消息发送和接收
- C#:一个增强的TcpListener(一)构造函数
- C#TCPClient应用-一个简单的消息发送和接收
- C#:一个增强的TcpListener(二)线程池
- C# Socket TcpClient 无法从传输连接中读取数据: 远程主机强迫关闭了一个现有的连接。。
- C#:一个增强的TcpListener(一)构造函数
- C#:一个增强的TcpListener(二)线程池
- C# 的TCPClient 异步连接与异步读数据
- C# TcpClient 由于目标机器积极拒绝,无法连接
- C# 超级简单的Telnet (TcpClient)客户端
- 一个简单的TCP客户端、服务端会话程序(C#)
- C# TcpClient 由于目标机器积极拒绝,无法连接