服务器端源码:

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号协议的更多相关文章

  1. Microsoft SQL Server 数据库 错误号大全

    panchzh :Microsoft SQL Server 数据库 错误号大全0 操作成功完成. 1 功能错误. 2 系统找不到指定的文件. 3 系统找不到指定的路径. 4 系统无法打开文件. 5 拒 ...

  2. GIN+GORILLA=A GOLANG WEBSOCKET SERVER

    鉴于聊天已然成为大部分app的基础功能,而大部分app用户基数有没有辣么大,常用的聊天server架构如xmpp或者消息队列实现之类的用起来还挺麻烦的,有比较难跟网页端做交互,加之H5标准落地,所以w ...

  3. swoole之建立 websocket server

    一.代码部分 <?php /** * 为什么用WebSocket? * HTTP的通信只能由客户端发起 * * WebSocket 协议是基于TCP的一种新的网络协议.实现了浏览器与服务器全双工 ...

  4. 查看SQL SERVER 加密存储过程,函数,触发器,视图

    原文:查看SQL SERVER 加密存储过程,函数,触发器,视图 create  PROCEDURE sp_decrypt(@objectname varchar(50))ASbeginset noc ...

  5. 使用Jetty搭建Java Websocket Server,实现图像传输

    https://my.oschina.net/yushulx/blog/298140 How to Implement a Java WebSocket Server for Image Transm ...

  6. SQL Server 加密案例解析

    一.概述 加密是一种安全措施,有时候甚至是法律要求.作为攻破Windows系统的最后一道防线,通过加密可以保证在没有密钥的情况下获取备份或者物理介质变得毫无意义. 二.概念 加密层次结构 加密层次结构 ...

  7. SpringBoot报错:Failed to load ApplicationContext(javax.websocket.server.ServerContainer not available)

    引起条件: WebSocket+单元测试,单元测试报错! 解决方法: SpringBootTest增加webEnvironment参数. https://docs.spring.io/spring-b ...

  8. 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 ...

  9. SQL SERVER孤立帐号的处理

    Step1:查询 Use KSHR_F23 Go exec sp_change_users_login @Action='Report' Go Step2:处理 Use KSHR_F23 Go exe ...

随机推荐

  1. N的阶乘的长度

    阶乘是乘法 , 乘法的话 , 几位数*几位数的位数 就是 哪两个几位数相加 .  这个可以用log10来解决 , 所以有如下代码 . #include<stdio.h> #include& ...

  2. Android Studio, gradle plugin is too old or set ANDROID_DAILY_OVERRIDE

    早上打开Android Studio,忽然报了个错,说gradle plugin版本过低,让更新或者设置ANDROID_DAILY_OVERRIDE环境变量: 日志如下: INFO - ls.idea ...

  3. HDU 5823 (状压dp)

    Problem color II 题目大意 定义一个无向图的价值为给每个节点染色使得每条边连接的两个节点颜色不同的最少颜色数. 对于给定的一张由n个点组成的无向图,求该图的2^n-1张非空子图的价值. ...

  4. C#指针转换

    下表显示了预定义的隐式指针转换. 隐式转换可能在多种情形下发生,包括调用方法时和在赋值语句中. 隐式指针转换       From To 任何指针类型 void* null 任何指针类型 显式指针转换 ...

  5. ajax 外部变量

    1.一般的js代码可以放在任何位置.但是用jquery写的代码需要先引入jquery文件,再写代码. 2.ajax函数中内部的变量不能传到外部.如果改变外部变量,需要async:false,代码如下: ...

  6. E - 今年暑假不AC

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Des ...

  7. LeetCode Kth Largest Element in an Array (快速排序)

    题意: 在一个无序的数组中第k大的数是多少? 思路: 按照快排的思路,如果每次分成两段后,设为L和R.如果R>=k ,则答案在右边集合,否则在左边集合. 这里用了3位取中法.注意快排别给写死循环 ...

  8. LeetCode Spiral Matrix II (技巧)

    题意: 从1开始产生连续的n2个数字,以螺旋的方式填满一个n*n的数组. 思路: 由于是填满一个矩阵,那么只需要每次都填一圈即可.应该注意特殊情况. 迭代: class Solution { publ ...

  9. java 中获取文件路径

    方案一: 文件目录如下: 配置文件:firehosetos3sample.properties在src目录下面第一层,与包是一层的 在Getpath_ClassLoader.java类中: Syste ...

  10. Pythono 实现 Permutation

    不管在R 还是python中,都有现成的函数来轻而易举地进行全排列(Permutation).无序排列等等.今天想要尝试一下使用自己写代码来实现全排列. 首先,我采用的算法如下: 对于一个数列 i.e ...