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 ...
随机推荐
- Aspose.Cells导出Excel(2)
DataTable dtTitle = ds.Tables[]; DataTable dtDetail = ds.Tables[]; int columns = dtTitle.Columns.Cou ...
- 使用BitArray判断素数
首先显示1024范围内的所有素数,然后显示输入的数是否是素数.1024 是代码中计算的素数的范围,可以修改.计算平方根,是为了确定一个基数的范围.1024的平方根是32,两个超过32 的数相乘,肯定大 ...
- Java判断字符串是否是数值
判断一个字符串是否是数值,可以用正则表达式来判断.更简单的方法是把字符串转换成Float或者Double,然后捕捉NumberFormatException错误,如果有错误,就说明不是一个数值,如果没 ...
- linux使用wkhtmltopdf报错error while loading shared libraries:
官网提示 linux需要这些动态库.depends on: zlib, fontconfig, freetype, X11 libs (libX11, libXext, libXrender) 在li ...
- 交易系统使用storm,在消息高可靠情况下,如何避免消息重复
概要:在使用storm分布式计算框架进行数据处理时,如何保证进入storm的消息的一定会被处理,且不会被重复处理.这个时候仅仅开启storm的ack机制并不能解决上述问题.那么该如何设计出一个好的方案 ...
- UICollectionViewCell定制Button
UICollectionViewCell定制Button 效果 特点 1.能够动态设置每行显示的按钮的个数,以及控件的摆放格式 2.实现单选或者多选的功能,实现点击事件 3.自定制按钮的显示样式 用法 ...
- GreenDao2.2升级GreenDao3.0的适配之路
前言.为什么要升级到Greendao3.0? 1. 多人开发 以往的数据库建表建Dao等操作要新开一个module,在统一的地方管理数据库建表,现在可以直接写Entity.多人开发时自己管自己的Ent ...
- android的消息提示(震动与提示音)
protected AudioManager audioManager; protected Vibrator vibrator; audioManager = (AudioManager)getSy ...
- iOS 性能调试
性能调优的方式: 1.通过专门的性能调优工具 2.通过代码优化 1. 性能调优工具: 下面针对iOS的性能调优工具进行一个介绍: 1.1 静态分析工具–Analyze 相信iOS开发者在App进行Bu ...
- 如何安装Genymotion虚拟机以及Genmotion的eclipse插件
---内容开始--- - 首先去genymotion的官网去下载其安装文件 资源下载 Genymotion官网必须注册一个账号这个账号安装之后还有用的,用户名最好用网易126邮箱注册----我下载的是 ...