c# ros
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的更多相关文章
- 阿里云VPS服务器,ROS内网穿透
Aliyun Windows Server 2008 R2中建立vpn服务器,ros中使用pptp拨号连接 2.在Aliyun服务器中,修改hosts,将内网分配的ip映射到指定的域名,在Aliyun ...
- QT下调试基于ros的catkin项目
1.首先告诉qt ros的搜索路径,通过修改qt creator 桌面启动程序来实现 sudo gedit ~/.local/share/applications/DigiaQtOpenSour ...
- ROS学习(三)—— ROS文件系统
一.预备工作 使用ros0tutorials程序包,先下载: sudo apt-get install ros-<distro>-ros-tutorials 其中<distro> ...
- ROS学习(二)—— 配置ROS环境
一.管理环境 p { margin-bottom: 0.25cm; line-height: 120% } a:link { } 如果你在查找和使用ROS软件包方面遇到了问题,请确保你已经正确配置了脚 ...
- ROS学习(一)—— 环境搭建
一.配置Ubuntu软件仓库且选择ROS正确版本 二.添加source.list sudo sh -c 'echo "deb http://packages.ros.org/ros/ubun ...
- [ROS] Studying Guidance
Reference: https://www.zhihu.com/question/35788789 安装指南:http://wiki.ros.org/indigo/Installation/Ubun ...
- ros::spin() 和 ros::spinOnce() 区别及详解
版权声明:本文为博主原创文章,转载请标明出处: http://www.cnblogs.com/liu-fa/p/5925381.html 博主提示:本文基于ROS Kinetic Kame,如有更(g ...
- Learning Roadmap of Robotic Operating System (ROS)
ROS Wiki: http://wiki.ros.org/ Robots Using ROS Textbooks: A Gentle Introduction to ROS Learning ROS ...
- k-develop 在ros上面的应用
sudo apt-get install kdevelop 根据wiki上面的ros 章节中,关于kdevelop的介绍,配置好环境即可. 导入工程时,选中src/src下面的章节,不过,需要注意去掉 ...
- ROS系统C++代码测试之gtest
1. 安装gtestsudo apt-get install libgtest-dev 2.修改CMakeLists.txtfind_package(GTest REQUIRED)uncommend ...
随机推荐
- 【无私分享:ASP.NET CORE 项目实战(第六章)】读取配置文件(一) appsettings.json
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 在我们之前的Asp.net mvc 开发中,一提到配置文件,我们不由的想到 web.config 和 app.config,在 ...
- Delphi_04_Delphi_Object_Pascal_基本语法_02
这里简单的描述Object的语法中的基本内容,数据类型.因为代码是自描述的所以不废话,直接贴代码. { 用户自定义类型 1.数组 2.动态数组 3.记录 4.集合 } program UserDefi ...
- IE 浏览器各个版本 JavaScript 支持情况一览表
语言元素 语言元素 突发.IE6 标准.IE7 标准 IE8 标准 IE 9 标准 IE 10 标准 边缘 Windows 应用商店应用程序 __proto__ 属性 (Object) (JavaSc ...
- Windows Form调用R进行绘图并显示
R软件功能非常强大,可以很好的进行各类统计,并能输出图形.下面介绍一种R语言和C#进行通信的方法,并将R绘图结果显示到WinForm UI界面上. 1 前提准备 安装R软件,需要安装32位的R软件,6 ...
- jQuery为开发插件提拱了两个方法:jQuery.fn.extend(); jQuery.extend();
jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); jQuery.fn jQuery.fn = jQuery.prototype ...
- jQuery+css3侧边栏导航菜单
效果体验:http://hovertree.com/texiao/jquery/37/ 代码如下: <!doctype html> <html lang="zh" ...
- 谈一谈前端多容器(多webview平台)处理方案
文中是我个人的一些开发经验,希望对各位有用,也希望各位多多支持讨论,指出文中不足以及提出您的一些建议. 双容器 得益于近几年移动端的发展,前端早已今非昔比,从大型框架来说angularJS.react ...
- C# Session添加、删除封装类
/// <summary> /// <para> </para> /// 常用工具类——Session操作类 /// <para> ---------- ...
- 阶段一:解析JSON
“阶段一”是指我第一次系统地学习Android开发.这主要是对我的学习过程作个记录. 最近学到解析JSON格式的网络数据,而作业也要求自己找一个天气预报的API地址,然后解析其中JSON格式的数据.可 ...
- 菜单(Menu)的三中创建方式——Android开发之路2
菜单的三种创建方式 一.OptionsMenu---选项菜单 Android应用中的菜单默认是隐藏的,只有当用户点击手机上的MENU键,系统才会显示菜单.这种菜单叫做选项菜单(Options Menu ...