Socket网络通信安全
2017-10-13 19:56
197 查看
前言
为了保证数据的安全性,在客户端服务器进行通信之前要对数据进行一次封装来保证数据的安全,在接受到数据后在进行拆包,就像是送快递之前要把里面的东西给包装好,在接到快点后在拆开. 压缩工具可以在网上下载ZipHelper开源的压缩帮助类 数据流读取,CRC16,Xor异或加密类可以直接使用 封装和拆包为示例
封装数据包:
* 先判断是否采用压缩,定义了一个ushort的常量,判断数据的长度大于这个常量就进行压缩,否则就不压缩 * 封装的方法采用了先压缩在异或加密最后进行CRC校验 * 完整的数据包是 "包头"+"包体"=完整数据包 * 包头是ushort类型的数据,存放包体的长度 * 包体包括 * Boll值--压缩的标志--1个字节 * ushrot类型的--CRC校验码--2个字节 * 加密后的数据
拆封数据包
(根据包头的包体长度拿到包体的数据之后进行拆封数据包)* 封装数据包时是先进行的压缩再异或加密最后CRC校验,拆包时就是反过来的 * 把包体的数据接到一个byte的数组中 * 包体的长度减去三个字节的长度就是经过压缩并异或的数据长度,用一个数组存放真实数据 * 把包体的所有数据都写入流中 * 用一个变量接受压缩标志 * 用一个ushort变量接受crc校验码 * 最后数据流中剩余的就是真实的数据 ,用前面定义好的数组接收 * 接收到真实数据后计算一下接受到的数据的crc校验码,判断传过来的校验码和计算数据的校验码是否一致,不一致就停止操作数据 * crc一致就证明数据是正确的,直接进行异或解密 * 再通过传输过来的压缩标志来进行判断是否有进行过压缩操作,有压缩就进行解压 * 最后拿到了最真实的数据,就通过传输协议进行获取数据
封包
/// <summary> /// 封装数据包 /// </summary> /// <param name="data"></param> /// <returns></returns> private byte[] MakeData(byte[] data) { byte[] retBuffer = null; //1.获取压缩标志 bool isCompress = data.Length > m_CompressLen ? true : false; //2.判断是否需要压缩 if (isCompress) data = ZlibHelper.CompressBytes(data); //3.获取真实数据异或加密后的数据 data = SecurityUtil.Xor(data); //4.获取Crc冗余校验码(压缩&&异或) ushort crc = Crc16.CalculateCrc16(data); Debug.Log("客户端CRC="+crc); using (MMO_MemoryStream ms = new MMO_MemoryStream()) { //5.包体长度写入数据流中 ms.WriteUShort((ushort)(data.Length + 3)); //6.压缩标志写入数据流中 ms.WriteBool(isCompress); //7.Crc冗余校验码写入数据流中 ms.WriteUShort(crc); //8.Xor加密后的数据写入流中 ms.Write(data, 0, data.Length); //9.获取Make后的完整数据包 retBuffer = ms.ToArray(); } return retBuffer; }
拆包
//存放接受到的队列中的包体(压缩标志|crc|Xor异或&&压缩后的数据(真实数据)) byte[] buffer = m_ReceiveQueue.Dequeue(); //存放Xor异或&&压缩后的数据 byte[] bufferNew = new byte[buffer.Length - 3]; //压缩标志 bool isCompress = false; //接受过来的Crc校验码 ushort crc = 0; //获取实际数据包体和协议编号 using (MMO_MemoryStream ms = new MMO_MemoryStream(buffer)) { //从数据流中读取压缩标志 isCompress = ms.ReadBool(); //从数据流中读取Crc校验码 crc = ms.ReadUShort(); //从数据流中读取Xor加密的数据 ms.Read(bufferNew, 0, bufferNew.Length); } //计算传输过来数据的Crc(压缩&&异或) int newCrc = Crc16.CalculateCrc16(bufferNew); //temp Debug.Log("服务器NewCRC=" + newCrc); if (newCrc == crc) { //异或得到原始数据 bufferNew = SecurityUtil.Xor(bufferNew); if (isCompress) //对数据进行解压缩 bufferNew = ZlibHelper.DeCompressBytes(bufferNew); ushort protoCode = 0; byte[] protoContent = new byte[bufferNew.Length - 2]; using (MMO_MemoryStream ms = new MMO_MemoryStream(bufferNew)) { //读取协议编号 protoCode = ms.ReadUShort(); //读取最原始的数据 ms.Read(protoContent, 0, protoContent.Length); //派发协议事件 EventDispatcher.Instance.Dispatch(protoCode, protoContent); } } else { break; }
Xor异或加密类
using System.Collections; using System.Collections.Generic; using UnityEngine; /// <summary> /// Socket Xor加密 /// </summary> public sealed class SecurityUtil { #region xorScale 异或因子 /// <summary> /// 异或因子 /// </summary> private static readonly byte[] xorScale = new byte[] { 45, 66, 38, 55, 23, 254, 9, 165, 90, 19, 41, 45, 201, 58, 55, 37, 254, 185, 165, 169, 19, 171 };//.data文件的xor加解密因子 #endregion private SecurityUtil() { } /// <summary> /// 对数据进行异或 /// </summary> /// <param name="buffer"></param> /// <returns></returns> public static byte[] Xor(byte[] buffer) { //xor加密 int iScaleLen = xorScale.Length; for (int i = 0; i < buffer.Length; i++) { buffer[i] = (byte)(buffer[i] ^ xorScale[i % iScaleLen]); } return buffer; } }
CRC冗余校验类
public class Crc16 { // Table of CRC values for high-order byte private static readonly byte[] _auchCRCHi = new byte[] { 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40, 0x01, 0xC0, 0x80, 0x41, 0x01, 0xC0, 0x80, 0x41, 0x00, 0xC1, 0x81, 0x40 }; // Table of CRC values for low-order byte private static readonly byte[] _auchCRCLo = new byte[] { 0x00, 0xC0, 0xC1, 0x01, 0xC3, 0x03, 0x02, 0xC2, 0xC6, 0x06, 0x07, 0xC7, 0x05, 0xC5, 0xC4 12704 , 0x04, 0xCC, 0x0C, 0x0D, 0xCD, 0x0F, 0xCF, 0xCE, 0x0E, 0x0A, 0xCA, 0xCB, 0x0B, 0xC9, 0x09, 0x08, 0xC8, 0xD8, 0x18, 0x19, 0xD9, 0x1B, 0xDB, 0xDA, 0x1A, 0x1E, 0xDE, 0xDF, 0x1F, 0xDD, 0x1D, 0x1C, 0xDC, 0x14, 0xD4, 0xD5, 0x15, 0xD7, 0x17, 0x16, 0xD6, 0xD2, 0x12, 0x13, 0xD3, 0x11, 0xD1, 0xD0, 0x10, 0xF0, 0x30, 0x31, 0xF1, 0x33, 0xF3, 0xF2, 0x32, 0x36, 0xF6, 0xF7, 0x37, 0xF5, 0x35, 0x34, 0xF4, 0x3C, 0xFC, 0xFD, 0x3D, 0xFF, 0x3F, 0x3E, 0xFE, 0xFA, 0x3A, 0x3B, 0xFB, 0x39, 0xF9, 0xF8, 0x38, 0x28, 0xE8, 0xE9, 0x29, 0xEB, 0x2B, 0x2A, 0xEA, 0xEE, 0x2E, 0x2F, 0xEF, 0x2D, 0xED, 0xEC, 0x2C, 0xE4, 0x24, 0x25, 0xE5, 0x27, 0xE7, 0xE6, 0x26, 0x22, 0xE2, 0xE3, 0x23, 0xE1, 0x21, 0x20, 0xE0, 0xA0, 0x60, 0x61, 0xA1, 0x63, 0xA3, 0xA2, 0x62, 0x66, 0xA6, 0xA7, 0x67, 0xA5, 0x65, 0x64, 0xA4, 0x6C, 0xAC, 0xAD, 0x6D, 0xAF, 0x6F, 0x6E, 0xAE, 0xAA, 0x6A, 0x6B, 0xAB, 0x69, 0xA9, 0xA8, 0x68, 0x78, 0xB8, 0xB9, 0x79, 0xBB, 0x7B, 0x7A, 0xBA, 0xBE, 0x7E, 0x7F, 0xBF, 0x7D, 0xBD, 0xBC, 0x7C, 0xB4, 0x74, 0x75, 0xB5, 0x77, 0xB7, 0xB6, 0x76, 0x72, 0xB2, 0xB3, 0x73, 0xB1, 0x71, 0x70, 0xB0, 0x50, 0x90, 0x91, 0x51, 0x93, 0x53, 0x52, 0x92, 0x96, 0x56, 0x57, 0x97, 0x55, 0x95, 0x94, 0x54, 0x9C, 0x5C, 0x5D, 0x9D, 0x5F, 0x9F, 0x9E, 0x5E, 0x5A, 0x9A, 0x9B, 0x5B, 0x99, 0x59, 0x58, 0x98, 0x88, 0x48, 0x49, 0x89, 0x4B, 0x8B, 0x8A, 0x4A, 0x4E, 0x8E, 0x8F, 0x4F, 0x8D, 0x4D, 0x4C, 0x8C, 0x44, 0x84, 0x85, 0x45, 0x87, 0x47, 0x46, 0x86, 0x82, 0x42, 0x43, 0x83, 0x41, 0x81, 0x80, 0x40 }; /// <summary> /// 获得CRC16效验码 /// </summary> /// <param name="buffer"></param> /// <returns></returns> public static ushort CalculateCrc16(byte[] buffer) { byte crcHi = 0xff; // high crc byte initialized byte crcLo = 0xff; // low crc byte initialized for (int i = 0; i < buffer.Length; i++) { int crcIndex = crcHi ^ buffer[i]; // calculate the crc lookup index crcHi = (byte)(crcLo ^ _auchCRCHi[crcIndex]); crcLo = _auchCRCLo[crcIndex]; } return (ushort)(crcHi << 8 | crcLo); } }
数据流读写类
using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using UnityEngine; using System; /// <summary> /// 数据转换(Byte short int long float decimal bool string) /// </summary> public class MMO_MemoryStream : MemoryStream { public MMO_MemoryStream() { } public MMO_MemoryStream(byte[] buffer) : base(buffer) { } #region Short /// <summary> /// 从流中读取一个short数据 /// </summary> /// <returns></returns> public short ReadShort() { byte[] arr = new byte[2]; base.Read(arr, 0, 2); return BitConverter.ToInt16(arr, 0); } /// <summary> /// 向流中写入一个short数组 /// </summary> /// <param name="value"></param> public void WriteShort(short value) { byte[] arr = BitConverter.GetBytes(value); base.Write(arr, 0, arr.Length); } #endregion #region UShort /// <summary> /// 从流中读取一个UShort数据 /// </summary> /// <returns></returns> public ushort ReadUShort() { byte[] arr = new byte[2]; base.Read(arr, 0, 2); return BitConverter.ToUInt16(arr, 0); } /// <summary> /// 向流中写入一个uShort数组 /// </summary> /// <param name="value"></param> public void WriteUShort(ushort value) { byte[] arr = BitConverter.GetBytes(value); base.Write(arr, 0, arr.Length); } #endregion #region Int /// <summary> /// 从流中读取一个int数据 /// </summary> /// <returns></returns> public int ReadInt() { byte[] arr = new byte[4]; base.Read(arr, 0, 4); return BitConverter.ToInt32(arr, 0); } /// <summary> /// 向流中写入一个int数组 /// </summary> /// <param name="value"></param> public void WriteInt(int value) { byte[] arr = BitConverter.GetBytes(value); base.Write(arr, 0, arr.Length); } #endregion #region UInit /// <summary> /// 从流中读取一个Uint数据 /// </summary> /// <returns></returns> public uint ReadUInt() { byte[] arr = new byte[4]; base.Read(arr, 0, 4); return BitConverter.ToUInt32(arr, 0); } /// <summary> /// 向流中写入一个uint数组 /// </summary> /// <param name="value"></param> public void WriteUInit(uint value) { byte[] arr = BitConverter.GetBytes(value); base.Write(arr, 0, arr.Length); } #endregion #region long /// <summary> /// 从流中读取一个long数据 /// </summary> /// <returns></returns> public long ReadLong() { byte[] arr = new byte[8]; base.Read(arr, 0, 8); return BitConverter.ToInt64(arr, 0); } /// <summary> /// 向流中写入一个long数组 /// </summary> /// <param name="value"></param> public void WriteLong(long value) { byte[] arr = BitConverter.GetBytes(value); base.Write(arr, 0, arr.Length); } #endregion #region ULong /// <summary> /// 从流中读取一个ULong数据 /// </summary> /// <returns></returns> public ulong ReadULong() { byte[] arr = new byte[8]; base.Read(arr, 0, 8); return BitConverter.ToUInt64(arr, 0); } /// <summary> /// 向流中写入一个ULong数组 /// </summary> /// <param name="value"></param> public void WriteULong(ulong value) { byte[] arr = BitConverter.GetBytes(value); base.Write(arr, 0, arr.Length); } #endregion #region Float /// <summary> /// 从流中读取一个float数据 /// </summary> /// <returns></returns> public float ReadFloat() { byte[] arr = new byte[4]; base.Read(arr, 0, 4); return BitConverter.ToSingle(arr, 0); } /// <summary> /// 向流中写入一个float数组 /// </summary> /// <param name="value"></param> public void WriteFloat(float value) { byte[] arr = BitConverter.GetBytes(value); base.Write(arr, 0, arr.Length); } #endregion #region Double /// <summary> /// 从流中读取一个Double数据 /// </summary> /// <returns></returns> public double ReadDouble() { byte[] arr = new byte[8]; base.Read(arr, 0, 8); return BitConverter.ToDouble(arr, 0); } /// <summary> /// 向流中写入一个double数组 /// </summary> /// <param name="value"></param> public void WriteDouble(double value) { byte[] arr = BitConverter.GetBytes(value); base.Write(arr, 0, arr.Length); } #endregion #region Bool /// <summary> /// 从流中读取一个Bool数据 /// </summary> /// <returns></returns> public bool ReadBool() { return base.ReadByte() == 1; } /// <summary> /// 向流中写入一个bool数组 /// </summary> /// <param name="value"></param> public void WriteBool(bool value) { base.WriteByte((byte)(value == true ? 1 : 0)); } #endregion #region UTF8String /// <summary> /// 从流中读取一个string数组 /// </summary> /// <returns></returns> public string ReadUTF8String() { ushort len = this.ReadUShort(); byte[] arr = new byte[len]; base.Read(arr, 0, len); return Encoding.UTF8.GetString(arr); } /// <summary> /// 把一个字符串数字写入流 /// </summary> /// <param name="str"></param> public void WriteUTF8String(string str) { if (string.IsNullOrEmpty(str)) return; byte[] arr = Encoding.UTF8.GetBytes(str); if (arr.Length > 65535) { throw new InvalidCastException("字符串超出范围"); } this.WriteUShort((ushort)arr.Length); base.Write(arr, 0, arr.Length); } #endregion }
相关文章推荐
- vc++/mfc socket网络通信多客户端和服务端安全问题,服务端只处理授权客户端
- 网络通信(4)--MulticastSocket
- Android网络开发之Socket通信
- 网络通信安全基础
- 企业信息与网络通信安全 团队成员简历-叶俊
- 20145219 《信息安全系统设计基础》实验五 网络通信
- Linux网络通信(二)Socket编写TCP/UDP
- 网络通信socket连接数上限
- 46.【IOS网络通信】socket第三方库 AsyncSocket(GCDAsyncSocket)
- 网络编程:使用Socket实现简单的服务器和客户端的通信
- CocoaAsyncSocket网络通信使用之tcp连接(一)
- UNIX环境高级编程学习之第十六章网络IPC:套接字 - 非阻塞的Socket通信Select模型(多路复用), 实用Socket通信模板。
- 【iOS网络通信】socket第三方库 AsyncSocket(GCDAsyncSocket)
- Linux网络编程之[Socket通信的常用函数简介]
- 详讲Socket网络通信建立应用更新,解包等操作
- JavaSE入门学习47:Socket网络通信编程(一)
- 工信部公布《通信网络安全防护监督管理办法(征求意见稿)》
- Java4Android之socket网络通信基础
- Java网络编程-用SSL构建安全的Socket
- [网络编程]——TCP_Socket通信_聊天室_客户端多线程.初步形成