我是靠谱客的博主 专注豌豆,这篇文章主要介绍Socket网络通信安全,现在分享给大家,希望可以做个参考。

前言

复制代码
1
2
3
4
为了保证数据的安全性,在客户端服务器进行通信之前要对数据进行一次封装来保证数据的安全,在接受到数据后在进行拆包,就像是送快递之前要把里面的东西给包装好,在接到快点后在拆开. 压缩工具可以在网上下载ZipHelper开源的压缩帮助类 数据流读取,CRC16,Xor异或加密类可以直接使用 封装和拆包为示例

封装数据包:

复制代码
1
2
3
4
5
6
7
8
9
* 先判断是否采用压缩,定义了一个ushort的常量,判断数据的长度大于这个常量就进行压缩,否则就不压缩 * 封装的方法采用了先压缩在异或加密最后进行CRC校验 * 完整的数据包是 "包头"+"包体"=完整数据包 * 包头是ushort类型的数据,存放包体的长度 * 包体包括 * Boll值--压缩的标志--1个字节 * ushrot类型的--CRC校验码--2个字节 * 加密后的数据

封装好的数据包格式

拆封数据包

(根据包头的包体长度拿到包体的数据之后进行拆封数据包)

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
* 封装数据包时是先进行的压缩再异或加密最后CRC校验,拆包时就是反过来的 * 把包体的数据接到一个byte的数组中 * 包体的长度减去三个字节的长度就是经过压缩并异或的数据长度,用一个数组存放真实数据 * 把包体的所有数据都写入流中 * 用一个变量接受压缩标志 * 用一个ushort变量接受crc校验码 * 最后数据流中剩余的就是真实的数据 ,用前面定义好的数组接收 * 接收到真实数据后计算一下接受到的数据的crc校验码,判断传过来的校验码和计算数据的校验码是否一致,不一致就停止操作数据 * crc一致就证明数据是正确的,直接进行异或解密 * 再通过传输过来的压缩标志来进行判断是否有进行过压缩操作,有压缩就进行解压 * 最后拿到了最真实的数据,就通过传输协议进行获取数据

封包

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/// <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; }

拆包

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//存放接受到的队列中的包体(压缩标志|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异或加密类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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冗余校验类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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,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, 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); } }

数据流读写类

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
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 }

最后

以上就是专注豌豆最近收集整理的关于Socket网络通信安全的全部内容,更多相关Socket网络通信安全内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(86)

评论列表共有 0 条评论

立即
投稿
返回
顶部