class MK
{
Stream connection;
TcpClient con; public MK(string ip)
{
con = new TcpClient();
con.Connect(ip, );
connection = (Stream)con.GetStream();
}
public void Close()
{
connection.Close();
con.Close();
}
public bool Login(string username, string password)
{
Send("/login", true);
string hash = Read()[].Split(new string[] { "ret=" }, StringSplitOptions.None)[];
Send("/login");
Send("=name=" + username);
//在此先对已加密的密码进行解密操作(为确保数据安全,在保存密码的时候,对密码进行了公钥加密)
//password=.......;
Send("=response=00" + EncodePassword(password, hash), true);
if (Read()[] == "!done")
{
return true;
}
else
{
return false;
}
}
public void Send(string co, bool endsentence=false)
{
byte[] bajty = Encoding.UTF8.GetBytes(co.ToCharArray());
byte[] velikost = EncodeLength(bajty.Length); connection.Write(velikost, , velikost.Length);
connection.Write(bajty, , bajty.Length);
if(endsentence)
{
connection.WriteByte();
}
}
public List<string> Read()
{
List<string> output = new List<string>();
string o = "";
byte[] tmp = new byte[];
long count;
while (true)
{
tmp[] = (byte)connection.ReadByte();
//if(tmp[3] == 220) tmp[3] = (byte)connection.ReadByte(); it sometimes happend to me that
//mikrotik send 220 as some kind of "bonus" between words, this fixed things, not sure about it though
if (tmp[] == )
{
output.Add(o);
if (o.Substring(, ) == "!done")
{
break;
}
else
{
o = "";
continue;
}
}
else
{
if (tmp[] < 0x80)
{
count = tmp[];
}
else
{
if (tmp[] < 0xC0)
{
int tmpi = BitConverter.ToInt32(new byte[] { (byte)connection.ReadByte(), tmp[], , }, );
count = tmpi ^ 0x8000;
}
else
{
if (tmp[] < 0xE0)
{
tmp[] = (byte)connection.ReadByte();
int tmpi = BitConverter.ToInt32(new byte[] { (byte)connection.ReadByte(), tmp[], tmp[], }, );
count = tmpi ^ 0xC00000;
}
else
{
if (tmp[] < 0xF0)
{
tmp[] = (byte)connection.ReadByte();
tmp[] = (byte)connection.ReadByte();
int tmpi = BitConverter.ToInt32(new byte[] { (byte)connection.ReadByte(), tmp[], tmp[], tmp[] }, );
count = tmpi ^ 0xE0000000;
}
else
{
if (tmp[] == 0xF0)
{
tmp[] = (byte)connection.ReadByte();
tmp[] = (byte)connection.ReadByte();
tmp[] = (byte)connection.ReadByte();
tmp[] = (byte)connection.ReadByte();
count = BitConverter.ToInt32(tmp, );
}
else
{
//Error in packet reception, unknown length
break;
}
}
}
}
}
} for (int i = ; i < count; i++)
{
o += (Char)connection.ReadByte();
}
}
return output;
}
byte[] EncodeLength(int delka)
{
if (delka < 0x80)
{
byte[] tmp = BitConverter.GetBytes(delka);
return new byte[] { tmp[] };
}
if (delka < 0x4000)
{
byte[] tmp = BitConverter.GetBytes(delka | 0x8000);
return new byte[] { tmp[], tmp[] };
}
if (delka < 0x200000)
{
byte[] tmp = BitConverter.GetBytes(delka | 0xC00000);
return new byte[] { tmp[], tmp[], tmp[] };
}
if (delka < 0x10000000)
{
byte[] tmp = BitConverter.GetBytes(delka | 0xE0000000);
return new byte[] { tmp[], tmp[], tmp[], tmp[] };
}
else
{
byte[] tmp = BitConverter.GetBytes(delka);
return new byte[] { 0xF0, tmp[], tmp[], tmp[], tmp[] };
}
} public string EncodePassword(string Password, string hash)
{
byte[] hash_byte = new byte[hash.Length / ];
for (int i = ; i <= hash.Length - ; i += )
{
hash_byte[i / ] = Byte.Parse(hash.Substring(i, ), System.Globalization.NumberStyles.HexNumber);
}
byte[] heslo = new byte[ + Password.Length + hash_byte.Length];
heslo[] = ;
Encoding.UTF8.GetBytes(Password.ToCharArray()).CopyTo(heslo, );
hash_byte.CopyTo(heslo, + Password.Length); Byte[] hotovo;
System.Security.Cryptography.MD5 md5; md5 = new System.Security.Cryptography.MD5CryptoServiceProvider(); hotovo = md5.ComputeHash(heslo); //Convert encoded bytes back to a 'readable' string
string navrat = "";
foreach (byte h in hotovo)
{
navrat += h.ToString("x2");
}
return navrat;
}
}

c# ros的更多相关文章

  1. 阿里云VPS服务器,ROS内网穿透

    Aliyun Windows Server 2008 R2中建立vpn服务器,ros中使用pptp拨号连接 2.在Aliyun服务器中,修改hosts,将内网分配的ip映射到指定的域名,在Aliyun ...

  2. QT下调试基于ros的catkin项目

    1.首先告诉qt ros的搜索路径,通过修改qt creator 桌面启动程序来实现 sudo    gedit ~/.local/share/applications/DigiaQtOpenSour ...

  3. ROS学习(三)—— ROS文件系统

    一.预备工作 使用ros0tutorials程序包,先下载: sudo apt-get install ros-<distro>-ros-tutorials 其中<distro> ...

  4. ROS学习(二)—— 配置ROS环境

    一.管理环境 p { margin-bottom: 0.25cm; line-height: 120% } a:link { } 如果你在查找和使用ROS软件包方面遇到了问题,请确保你已经正确配置了脚 ...

  5. ROS学习(一)—— 环境搭建

    一.配置Ubuntu软件仓库且选择ROS正确版本 二.添加source.list sudo sh -c 'echo "deb http://packages.ros.org/ros/ubun ...

  6. [ROS] Studying Guidance

    Reference: https://www.zhihu.com/question/35788789 安装指南:http://wiki.ros.org/indigo/Installation/Ubun ...

  7. ros::spin() 和 ros::spinOnce() 区别及详解

    版权声明:本文为博主原创文章,转载请标明出处: http://www.cnblogs.com/liu-fa/p/5925381.html 博主提示:本文基于ROS Kinetic Kame,如有更(g ...

  8. Learning Roadmap of Robotic Operating System (ROS)

    ROS Wiki: http://wiki.ros.org/ Robots Using ROS Textbooks: A Gentle Introduction to ROS Learning ROS ...

  9. k-develop 在ros上面的应用

    sudo apt-get install kdevelop 根据wiki上面的ros 章节中,关于kdevelop的介绍,配置好环境即可. 导入工程时,选中src/src下面的章节,不过,需要注意去掉 ...

  10. ROS系统C++代码测试之gtest

    1. 安装gtestsudo apt-get install libgtest-dev 2.修改CMakeLists.txtfind_package(GTest REQUIRED)uncommend ...

随机推荐

  1. C# WinForm国际化的简单实现

    软件行业发展到今天,国际化问题一直都占据非常重要的位置,而且应该越来越被重视.对于开发人员而言,在编写程序之前,国际化问题是首先要考虑的一个问题,也许有时候这个问题已经在设计者的考虑范围之内,但终归要 ...

  2. Springboot框架

    本片文章主要分享一下,Springboot框架为什么那么受欢迎以及如何搭建一个Springboot框架. 我们先了解一下Springboot是个什么东西,它是干什么用的.我是刚开始接触,查了很多资料, ...

  3. 数据结构:栈 顺序表方法和单链表方法(python版)

    #!/usr/bin/env python # -*- coding:utf-8 -*- class StackUnderflow(ValueError): pass #链表节点 class Node ...

  4. 【工匠大道】 svn命令自己总结

     本文地址   分享提纲: 1. svn 不常见单有用的命令 2. svn查看切换用户 1. svn自己总结的一些不常见,但有用的命令 1)[导出svn不带版本代码]导出不带svn版本控制的代码到本地 ...

  5. PowerDesigner从Sqlserver中反转为带注释的字典及快捷键操作

    PowerDesigner的操作经常忘记,所以把常用的功能记录下来备忘. 1.修改反转过来的字段 PowerDesigner从数据库反转的时候,默认不带注释,需要先进行修改. 输入如下脚本: {OWN ...

  6. jQuery fsBanner 手风琴

    fsbanner是一款自定义功能丰富的响应式网站Banner手风琴特效jQuery插件.该手风琴特效兼容性很好,支持点击和鼠标滑过等触发事件,并且可添加标题或描述. 在线实例 默认 带标题 鼠标滑过 ...

  7. android的消息提示(震动与提示音)

    protected AudioManager audioManager; protected Vibrator vibrator; audioManager = (AudioManager)getSy ...

  8. 【代码笔记】iOS-实现网络图片的异步加载和缓存

    代码: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. se ...

  9. Appfuse:第一张表维护

    1. 建立表userinfo 列名 描述 UserID 主键.自增 UserName 用户名 Pwd 密码 CreateDate 创建日期 2. 在src/main/resources目录下增加文件h ...

  10. C# List 排序各种用法与比较

    下面介绍各种List的sort的用法与比较 首先,我们建一个People的实体,有name.age.sex的属性,我们要排序的字段是年龄age 新建一个实体类 public class People ...