C# websocket Server 加密 76号协议
服务器端源码:
76号协议增加了加密字段
sec-websocket-key1
sec-websocket-key2
以及最后8个字节
服务器必须在握手信息之后发送回解密信息才能握手成功。
解密方式
key1 是 sec-websocket-key1 后面所有字节
key2 是 sec-websocket-key2 后面所有字节
part1 为 key1内除去所有的非数字字符后得到的数字 long型 / key1内空字符长度 int型
part2 为 key2内除去所有的非数字字符后得到的数字 long型 / key1内空字符长度 int型
part1 转换为byte[] byte1 (例:byte1 = {1,2,3,4})
part2 转换为byte[] byte2
byte1、byte2内值倒转(例:byte1 = {4,3,2,1})
byte3 是 客户端发送过来的最后8个字节
byte1、byte2、byte3 拼装为16个字节 byteKey[16]
byteKey[] 进行 MD5 加密 得 byteMD5[]
byteMD5[] 放置在握手协议最后端发送回去
C# cs:
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Security.Cryptography;
using System.Web.Security;
using System.IO;
namespace Room
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("服务器启动中...");
IPEndPoint ipe = new IPEndPoint(IPAddress.Parse("192.168.1.80"), 2000);
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Bind(ipe);
socket.Listen(60);
Console.WriteLine("服务器启动完毕...");
bool isfirst = true;
bool run = true;
try
{
Socket a = socket.Accept();
while (run)
{
byte[] bytes = new byte[1024];
int bytelength = a.Receive(bytes);
Console.WriteLine("接收完毕。");
#region websocket握手
if (isfirst)
{
string recStr = Encoding.UTF8.GetString(bytes, 0, bytelength);
string[] ss = recStr.Split(Environment.NewLine.ToCharArray());
string k1 = ss[10].Substring(20);
string k2 = ss[12].Substring(20);
byte[] last = new byte[8];
for (int i = 0; i < 8; i++)
{
last[i] = bytes[bytelength - 8 + i];
}
uint key1 = (uint)(long.Parse(FilterNonNumber(k1)) / FilterNonEmpty(k1).Length);
uint key2 = (uint)(long.Parse(FilterNonNumber(k2)) / FilterNonEmpty(k2).Length);
byte[] byteTemp1 = BitConverter.GetBytes(key1);
byte[] byteKey1 = new byte[4];
byte[] byteTemp2 = BitConverter.GetBytes(key2);
byte[] byteKey2 = new byte[4];
for (int i = 0; i < 4; i++)
{
byteKey1[i] = byteTemp1[3 - i];
byteKey2[i] = byteTemp2[3 - i];
}
MemoryStream ms = new MemoryStream();
ms.Write(byteKey1, 0, 4);
ms.Write(byteKey2, 0, 4);
ms.Write(last, 0, 8);
ms.Flush();
byte[] byteMs = ms.ToArray();
ms.Dispose();
MD5 md5 = MD5.Create();
byte[] md = md5.ComputeHash(byteMs);
MemoryStream ms1 = new MemoryStream();
string sendStr = "HTTP/1.1 101 WebSocket Protocol Handshake/r/nUpgrade: WebSocket/r/nConnection: Upgrade/r/nSec-WebSocket-Origin: null/r/nSec-WebSocket-Location: ws://192.168.1.80:2000//r/n/r/n";
byte[] temp = Encoding.UTF8.GetBytes(sendStr);
ms1.Write(temp, 0, temp.Length);
ms1.Write(md, 0, md.Length);
ms1.Flush();
byte[] byteSend = ms1.ToArray();
ms1.Dispose();
a.Send(byteSend, byteSend.Length, 0);
Console.WriteLine("发送完毕。");
isfirst = false;
}
#endregion
else
{
if (bytelength > 2)
{
string recStr = Encoding.UTF8.GetString(bytes, 1, bytelength - 2);
MemoryStream ms2 = new MemoryStream();
string strSend = "发送回去的是中文。";
byte[] byteSend = Encoding.UTF8.GetBytes(strSend);
ms2.WriteByte(0);
ms2.Write(byteSend, 0, byteSend.Length);
ms2.WriteByte(255);
ms2.Flush();
a.Send(ms2.ToArray());
ms2.Dispose();
if (recStr.Equals("exit"))
{
run = false;
Console.WriteLine("用户已退出。");
}
else
{
Console.WriteLine("接收到:" + recStr);
}
}
else
{
run = false;
Console.WriteLine("用户已退出。");
}
}
}
Console.ReadLine();
a.Close();
socket.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{
socket.Close();
}
}
public static string FilterNonNumber(string str)
{
if (str == null)
{
return "";
}
char[] c = str.ToCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0, len = c.Length; i < len; i++)
{
if (char.IsNumber(c[i]))
{
sb.Append(c[i]);
}
}
return sb.ToString();
}
public static string FilterNonEmpty(string str)
{
if (str == null)
{
return " ";
}
char[] c = str.ToCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0, len = c.Length; i < len; i++)
{
if (c[i] == ' ')
{
sb.Append(c[i]);
}
}
if (sb.ToString().Length == 0)
{
return " ";
}
else
{
return sb.ToString();
}
}
}
}
C# websocket Server 加密 76号协议的更多相关文章
- Microsoft SQL Server 数据库 错误号大全
panchzh :Microsoft SQL Server 数据库 错误号大全0 操作成功完成. 1 功能错误. 2 系统找不到指定的文件. 3 系统找不到指定的路径. 4 系统无法打开文件. 5 拒 ...
- GIN+GORILLA=A GOLANG WEBSOCKET SERVER
鉴于聊天已然成为大部分app的基础功能,而大部分app用户基数有没有辣么大,常用的聊天server架构如xmpp或者消息队列实现之类的用起来还挺麻烦的,有比较难跟网页端做交互,加之H5标准落地,所以w ...
- swoole之建立 websocket server
一.代码部分 <?php /** * 为什么用WebSocket? * HTTP的通信只能由客户端发起 * * WebSocket 协议是基于TCP的一种新的网络协议.实现了浏览器与服务器全双工 ...
- 查看SQL SERVER 加密存储过程,函数,触发器,视图
原文:查看SQL SERVER 加密存储过程,函数,触发器,视图 create PROCEDURE sp_decrypt(@objectname varchar(50))ASbeginset noc ...
- 使用Jetty搭建Java Websocket Server,实现图像传输
https://my.oschina.net/yushulx/blog/298140 How to Implement a Java WebSocket Server for Image Transm ...
- SQL Server 加密案例解析
一.概述 加密是一种安全措施,有时候甚至是法律要求.作为攻破Windows系统的最后一道防线,通过加密可以保证在没有密钥的情况下获取备份或者物理介质变得毫无意义. 二.概念 加密层次结构 加密层次结构 ...
- SpringBoot报错:Failed to load ApplicationContext(javax.websocket.server.ServerContainer not available)
引起条件: WebSocket+单元测试,单元测试报错! 解决方法: SpringBootTest增加webEnvironment参数. https://docs.spring.io/spring-b ...
- io.undertow.websockets.jsr.ServerWebSocketContainer cannot be cast to org.apache.tomcat.websocket.server.WsServerContainer
Caused by: java.lang.ClassCastException: io.undertow.websockets.jsr.ServerWebSocketContainer cannot ...
- SQL SERVER孤立帐号的处理
Step1:查询 Use KSHR_F23 Go exec sp_change_users_login @Action='Report' Go Step2:处理 Use KSHR_F23 Go exe ...
随机推荐
- BestCoder Round #1
逃生 反向拓扑+优先队列+逆序输出 这里要注意,题中要求的不是输出字典序,而是要编号小的尽量考前(首先1尽量考前,然后2尽量考前..). 比如说 约束是 4->1,3->2,字典序答案就是 ...
- BZOJ 3999 旅游
.......好长啊. #include<iostream> #include<cstdio> #include<cstring> #include<algo ...
- Windows10中无法打开这个应用的解决方案
报错的图:
- Finding Nemo_BFS
Description Nemo is a naughty boy. One day he went into the deep sea all by himself. Unfortunately, ...
- inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效
inno安装卸载时检测程序是否正在运行卸载完成后自动打开网页-代码无效 inno setup 安装卸载时检测程序是佛正在运行卸载完成后自动打开网页-代码无效 --------------------- ...
- magento -- 添加中国省份列表
magento本身的数据库里不包含中国的省份信息,你可以执行以下的SQL语句来添加 添加到directory_country_region表 INSERT INTO `directory_count ...
- 第一个sprint与第二个sprint 阶段总结
总体来说: 1.团队合作情况: 整个队伍配合得还算是不错的了,有些队员比较积极与主动,这是比较好的现象,因为一个队伍最终要的是活力与团结,至少我是这样认为的,如果一个队伍每个人都不干活,我想后果是不堪 ...
- nginx+c+cgi开发
http://blog.csdn.net/marising/article/details/3932938 1.Nginx 1.1.安装 Nginx 的中文维基 http://wiki.codemon ...
- CSS 实现:两栏布局(一边固定,一边自适应)
☊[实现要求]:CSS实现左边固定,右边自适应父容器宽度的两栏布局. <body> <div class="left"></div> <d ...
- ng-init