| | |
| | | using System;
|
| | | using System.Collections.Generic;
|
| | | using System.Security.Cryptography;
|
| | | using System.Text;
|
| | |
|
| | | namespace Shared.Phone.UserCenter
|
| | |
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 当前Wifi的Ip地址
|
| | | /// </summary>
|
| | | public string IpAddress
|
| | | {
|
| | | get
|
| | | {
|
| | | var temp = new Shared.Net.NetWiFi();
|
| | | return temp.GetIPAddress();
|
| | | }
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 当前是否是网关热点
|
| | | /// </summary>
|
| | | public bool IsGatewayHotspot
|
| | | {
|
| | | get
|
| | | {
|
| | | string myId = this.SSID;
|
| | |
|
| | | if (myId.StartsWith("hdlZigbeeGW") == true)
|
| | | {
|
| | | return true;
|
| | | }
|
| | | return false;
|
| | | }
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 当前wifi的SSID,也叫Wifi的名字(取不到时,会返回null)
|
| | | /// </summary>
|
| | | public string SSID
|
| | |
| | | {
|
| | | System.Threading.Thread.Sleep(50);
|
| | | count++;
|
| | | if (count == 60)
|
| | | if (count == 20)
|
| | | {
|
| | | //3秒超时
|
| | | //1秒超时
|
| | | return null;
|
| | | }
|
| | | }
|
| | |
| | | private Com.Mediatek.Elian.ElianNative hdlWiFi = null;
|
| | | #endif
|
| | |
|
| | | /// <summary>
|
| | | /// TCP协议
|
| | | /// </summary>
|
| | | private System.Net.Sockets.TcpClient tcpClient = null;
|
| | | /// <summary>
|
| | | /// 链接流对象
|
| | | /// </summary>
|
| | | private System.Net.Sockets.NetworkStream networkStream = null;
|
| | | /// <summary>
|
| | | /// Ap模式网关返回的秘钥
|
| | | /// </summary>
|
| | | private string randDesKey = null;
|
| | |
|
| | | #endregion
|
| | |
|
| | | #region ■ Ap模式的TCP链接____________________
|
| | |
|
| | | /// <summary>
|
| | | /// 打开Ap模式的TCP链接(有错误时,直接返回错误消息。没有错误时,返回null)
|
| | | /// </summary>
|
| | | /// <returns></returns>
|
| | | public string StartApTcpConnection()
|
| | | {
|
| | | try
|
| | | {
|
| | | //TCP连接
|
| | | tcpClient = new System.Net.Sockets.TcpClient();
|
| | | tcpClient.Connect("192.168.8.1", 5000);
|
| | | networkStream = tcpClient.GetStream();
|
| | | //加密数据
|
| | | string sendDataMsg = "hdl1985.";
|
| | | string DefaultdesKey = "hdl1985.";
|
| | | //44bytes 加密数据 DES加密 base64密文
|
| | | var sendData = Encoding.UTF8.GetBytes(DesEncrypt(sendDataMsg, DefaultdesKey));
|
| | | //将数据写入网络流
|
| | | networkStream.Write(sendData, 0, sendData.Length);
|
| | |
|
| | | System.Threading.Thread.Sleep(500);
|
| | | //2.接收状态,长度<1024字节
|
| | | byte[] bytes = new Byte[1024];
|
| | | int length = networkStream.Read(bytes, 0, bytes.Length);
|
| | | if (length == 0)
|
| | | {
|
| | | //通信链接失败
|
| | | return Language.StringByID(R.MyInternationalizationString.uCommunicationLinkFail);
|
| | | }
|
| | | string data = Encoding.UTF8.GetString(bytes, 0, length);
|
| | | //网关返回的秘钥
|
| | | this.randDesKey = DESDecrypt(data, DefaultdesKey);
|
| | |
|
| | | return null;
|
| | | }
|
| | | catch (Exception ex)
|
| | | {
|
| | | HdlLogLogic.Current.WriteLog(ex);
|
| | | this.CloseApTcpConnection();
|
| | | //通信链接失败
|
| | | return Language.StringByID(R.MyInternationalizationString.uCommunicationLinkFail);
|
| | | }
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// Ap模式 发送密码到家庭的路由器
|
| | | /// </summary>
|
| | | /// <param name="ssid"></param>
|
| | | /// <param name="password"></param>
|
| | | /// <returns></returns>
|
| | | public bool SendApHomeWifiPassword(string ssid, string password)
|
| | | {
|
| | | try
|
| | | {
|
| | | //加密数据
|
| | | string data2 = "ssid:" + ssid + "," + "pwd:" + password;
|
| | | var sendData2 = Encoding.UTF8.GetBytes(DesEncrypt(data2, randDesKey));
|
| | | //将数据写入网络流
|
| | | networkStream.Write(sendData2, 0, sendData2.Length);
|
| | | return true;
|
| | | }
|
| | | catch (Exception ex)
|
| | | {
|
| | | HdlLogLogic.Current.WriteLog(ex);
|
| | | return false;
|
| | | }
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 关闭Ap模式的tcp链接
|
| | | /// </summary>
|
| | | public void CloseApTcpConnection()
|
| | | {
|
| | | try
|
| | | {
|
| | | //关闭客户端
|
| | | tcpClient?.Close();
|
| | | tcpClient = null;
|
| | | networkStream = null;
|
| | | randDesKey = null;
|
| | | }
|
| | | catch (Exception ex)
|
| | | {
|
| | | HdlLogLogic.Current.WriteLog(ex);
|
| | | }
|
| | | }
|
| | |
|
| | | #endregion
|
| | |
|
| | | #region ■ 加密解密___________________________
|
| | |
|
| | | /// <summary>
|
| | | /// //加密数据
|
| | | /// </summary>
|
| | | /// <param name="pToDesEncrypt"></param>
|
| | | /// <param name="sKey">DES秘钥</param>
|
| | | /// <returns></returns>
|
| | | private string DesEncrypt(string pToDesEncrypt, string sKey)
|
| | | {
|
| | | // 创建des加密没
|
| | | using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
|
| | | {
|
| | | // 得到UTF-8的数据源
|
| | | byte[] inputByteArray = Encoding.UTF8.GetBytes(pToDesEncrypt);
|
| | | // 设置key和iv密钥
|
| | | des.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
|
| | | des.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
|
| | | //由于java是使用默认加密模式,C#默认是CBC,需要修改为ECB
|
| | | des.Mode = CipherMode.CBC;
|
| | | des.Padding = PaddingMode.PKCS7;
|
| | | // 创建数据流并进行加密
|
| | | var ms = new System.IO.MemoryStream();
|
| | | using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write))
|
| | | {
|
| | | cs.Write(inputByteArray, 0, inputByteArray.Length);
|
| | | cs.FlushFinalBlock();
|
| | | cs.Close();
|
| | | }
|
| | | // 关闭数据流
|
| | | ms.Close();
|
| | | return Convert.ToBase64String(ms.ToArray());//base64密文
|
| | | }
|
| | | }
|
| | |
|
| | | /// <summary>
|
| | | /// 解密
|
| | | /// </summary>
|
| | | /// <param name="pToDecrypt"></param>
|
| | | /// <param name="key">解密密匙</param>
|
| | | /// <returns></returns>
|
| | | private string DESDecrypt(string pToDecrypt, string key = null)
|
| | | {
|
| | | byte[] inputByteArray = Convert.FromBase64String(pToDecrypt);
|
| | | using (DESCryptoServiceProvider des = new DESCryptoServiceProvider())
|
| | | {
|
| | |
|
| | | des.Key = ASCIIEncoding.ASCII.GetBytes(key);
|
| | | des.IV = ASCIIEncoding.ASCII.GetBytes(key);
|
| | |
|
| | | var ms = new System.IO.MemoryStream();
|
| | | using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write))
|
| | | {
|
| | | cs.Write(inputByteArray, 0, inputByteArray.Length);
|
| | | cs.FlushFinalBlock();
|
| | | cs.Close();
|
| | | }
|
| | | string str = Encoding.UTF8.GetString(ms.ToArray());
|
| | | ms.Close();
|
| | | return str;
|
| | | }
|
| | | }
|
| | |
|
| | | #endregion
|
| | |
|
| | | #region ■ 一般方法___________________________
|