Example #1
0
        /// <summary>
        /// 测试激活
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_Click_16(object sender, RoutedEventArgs e)
        {
            SetterDAO setterDAO = new SetterDAO();

            //获取mac地址
            StringBuilder stringBuilder = new StringBuilder();
            //string strMac = CommUtil.GetMacAddress();
            // List<string> Macs = CommUtil.GetMacByWMI();
            List <string> Macs = CommUtil.GetMacByIPConfig();

            foreach (string mac in Macs)
            {
                string prefix = "物理地址. . . . . . . . . . . . . : ";
                string Mac    = mac.Substring(prefix.Length - 1);
                stringBuilder.Append(Mac);
            }
            //Console.WriteLine("==================="+stringBuilder.ToString());
            //MessageBox.Show("===================" + stringBuilder.ToString());
            entity.Setter setter = new entity.Setter();
            //mac地址先变为byte[]再aes加密
            byte[] byteMac = Encoding.GetEncoding("GBK").GetBytes(stringBuilder.ToString());
            byte[] AesMac  = AesUtil.Encrypt(byteMac, ProtocolConstant.USB_DOG_PASSWORD);
            //存入数据库
            //setter.Set_Unique_Id = Encoding.GetEncoding("GBK").GetString(AesMac);
            setter.Set_Unique_Id = ProtocolUtil.BytesToString(AesMac);

            /*AES解密
             * byte[] a = ProtocolUtil.StringToBcd(setter.Set_Unique_Id);
             * byte[] b = AesUtil.Decrypt(a, ProtocolConstant.USB_DOG_PASSWORD);
             * Console.WriteLine(Encoding.GetEncoding("GBK").GetString(b));*/
            //默认照片路径,激活时获取(路径中不要有汉字)
            string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
            string path     = ConfigurationManager.AppSettings["PicPath"];

            setter.Set_PhotoLocation = basePath + path;
            setter.Set_Language      = 1;
            setter.Pk_Set_Id         = 1;
            //设置版本号
            setter.Set_Version = CommUtil.GetCurrentVersion();
            if (!Directory.Exists(@setter.Set_PhotoLocation))
            {
                Directory.CreateDirectory(@setter.Set_PhotoLocation);//不存在就创建目录
            }

            /*if (Directory.Exists(@setter.Set_PhotoLocation)) {  //存在就删除
             *  Directory.Delete(@setter.Set_PhotoLocation, true);
             *  Directory.CreateDirectory(@setter.Set_PhotoLocation);
             * }*/
            setterDAO.InsertOneMacAdress(setter);
        }
Example #2
0
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            //打包协议到result;
            byte[] result = null;
            ProtocolConstant.USB_DOG_CONTENT = new byte[] { 0x0A, 0x0B, 0x0C };
            new MakerUSBDogFrame().PackData(ref result, new byte[] { 0xFF }, ProtocolConstant.USB_DOG_CONTENT);
            string a = ProtocolUtil.BytesToString(result);

            MessageBox.Show(a);

            //解析这个协议
            object rr = null;

            new ParserUSBDogFrame().Parser(ref rr, result);
            string b = ProtocolUtil.BytesToString((byte[])rr);

            //解析出的数据体
            MessageBox.Show(b);
            //是否和发送的数据体相等
            MessageBox.Show(ProtocolUtil.ArrayEqual((byte[])rr, ProtocolConstant.USB_DOG_CONTENT).ToString());
        }
        private void Btn_Activate(object sender, RoutedEventArgs e)
        {
            //InputNonPublicInformationPassword
            InputNonPublicInformationPassword passwordInput = new InputNonPublicInformationPassword
            {
                Owner                 = Window.GetWindow(this),
                ShowActivated         = true,
                ShowInTaskbar         = false,
                WindowStartupLocation = WindowStartupLocation.CenterScreen
            };

            passwordInput.ShowDialog();
            if (ProtocolConstant.USB_SUCCESS == 0) //u盘成功读取 破解状态为0
            {                                      //获取mac地址
                StringBuilder stringBuilder = new StringBuilder();
                //string strMac = CommUtil.GetMacAddress();
                // List<string> Macs = CommUtil.GetMacByWMI();
                List <string> Macs = CommUtil.GetMacByIPConfig();
                foreach (string mac in Macs)
                {
                    string prefix = "物理地址. . . . . . . . . . . . . : ";
                    string Mac    = mac.Substring(prefix.Length - 1);
                    stringBuilder.Append(Mac);
                }
                //Console.WriteLine("==================="+stringBuilder.ToString());
                //MessageBox.Show("===================" + stringBuilder.ToString());
                entity.Setter setter = new entity.Setter();
                //mac地址先变为byte[]再aes加密
                byte[] byteMac = Encoding.GetEncoding("GBK").GetBytes(stringBuilder.ToString());
                byte[] AesMac  = AesUtil.Encrypt(byteMac, ProtocolConstant.USB_DOG_PASSWORD);
                //存入数据库
                //setter.Set_Unique_Id = Encoding.GetEncoding("GBK").GetString(AesMac);
                setter.Set_Unique_Id = ProtocolUtil.BytesToString(AesMac);

                /*AES解密
                 * byte[] a = ProtocolUtil.StringToBcd(setter.Set_Unique_Id);
                 * byte[] b = AesUtil.Decrypt(a, ProtocolConstant.USB_DOG_PASSWORD);
                 * Console.WriteLine(Encoding.GetEncoding("GBK").GetString(b));*/
                //默认照片路径,激活时获取(路径中不要有汉字)
                string basePath = System.AppDomain.CurrentDomain.BaseDirectory;
                string path     = ConfigurationManager.AppSettings["PicPath"];
                setter.Set_PhotoLocation = basePath + path;
                setter.Set_Language      = 1;
                //默认备份路径,激活时获取
                setter.Back_Up      = basePath + @"BackUp\";
                setter.Set_Language = 1;
                //设置版本号
                setter.Set_Version = CommUtil.GetCurrentVersion();
                if (!Directory.Exists(@setter.Set_PhotoLocation))
                {
                    Directory.CreateDirectory(@setter.Set_PhotoLocation);//不存在就创建目录
                }

                /*if (Directory.Exists(@setter.Set_PhotoLocation)) {  //存在就删除
                 *  Directory.Delete(@setter.Set_PhotoLocation, true);
                 *  Directory.CreateDirectory(@setter.Set_PhotoLocation);
                 * }*/
                SetterDAO.InsertOneMacAdress(setter);
                //注释的部分为添加多个mac地址
                // List<entity.Setter> ListMac = CommUtil.GetMacByWMI();
                // SetterDAO.InsertMacAdress(ListMac);



                Status.Content = LanguageUtils.GetCurrentLanuageStrByKey("SettingsView.Activated");
                Color color = Color.FromArgb(255, 2, 200, 5);
                Status.Foreground     = new SolidColorBrush(color);
                BtnActivite.IsEnabled = false;
            }
            else
            {
                MessageBoxX.Error(LanguageUtils.ConvertLanguage("激活失败", "Activation fails"));
            }
        }
Example #4
0
        public byte[] Parser(byte[] source)
        {
            byte[] response = new byte[0];

            byte[] buffer   = ProtocolUtil.UnTransfer(source);
            MsgId  msgId    = ProtocolUtil.BytesToMsgId(buffer, 0);
            Int16  data_len = BitConverter.ToInt16(buffer, 2);

            data_len = IPAddress.NetworkToHostOrder(data_len);
            string terminalId = ProtocolUtil.BcdToString(buffer, 4, 6);
            Int16  serialNo   = BitConverter.ToInt16(buffer, 10);

            serialNo = IPAddress.NetworkToHostOrder(serialNo);
            byte xor = buffer[12 + data_len];

            //数据体
            byte[] data = new byte[data_len];

            Array.Copy(buffer, 12, data, 0, data_len);

            byte calcXor = ProtocolUtil.XorByByte(buffer, 0, 12 + data_len);

            TcpFrameBean frameBean = new TcpFrameBean();

            frameBean.DataBody   = data;
            frameBean.SerialNo   = serialNo;
            frameBean.TerminalId = terminalId;
            frameBean.MsgId      = msgId;
            frameBean.DeviceType = (DeviceType)buffer[4];
            if (xor != calcXor)
            {
                logger.Error("校验码不符合预期:" + ProtocolUtil.BytesToString(source));

                frameBean.AppendErrorMsg("校验码不符合预期");
                byte[] d = MakerTCPFrame.GetInstance().Make8001Frame(serialNo, MsgId.X0001, CommResponse.MistakeMsg);
                response = MakerTCPFrame.GetInstance().PackData(MsgId.X8001, frameBean.TerminalId, d);
                return(response);
            }

            switch (msgId)
            {
            case MsgId.X0001:    //设备通用应答
                break;

            case MsgId.X0002:    //心跳OK
                logger.Info("收到心跳:" + ProtocolUtil.BytesToString(source));
                WriteLogFile("收到心跳:" + ProtocolUtil.BytesToString(source));
                HandleHeartBeat(ref response, frameBean);
                break;

            case MsgId.X0008:    //开始训练
                logger.Info("收到开始训练请求:" + ProtocolUtil.BytesToString(source));
                WriteLogFile("收到开始训练请求:" + ProtocolUtil.BytesToString(source));
                HandleStartPrictice(ref response, frameBean);
                break;

            case MsgId.X0009:    //训练结果上报
                logger.Info("收到训练结果上报:" + ProtocolUtil.BytesToString(source));
                WriteLogFile("收到训练结果上报:" + ProtocolUtil.BytesToString(source));
                byte[] dd = MakerTCPFrame.GetInstance().Make8001Frame(serialNo, MsgId.X0001, CommResponse.Success);
                response = MakerTCPFrame.GetInstance().PackData(MsgId.X8001, frameBean.TerminalId, dd);
                HandlePricticeResult(ref response, frameBean);
                break;

            case MsgId.X000A:    //请求使用者信息
                logger.Info("收到请求使用者信息:" + ProtocolUtil.BytesToString(source));
                WriteLogFile("收到请求使用者信息:" + ProtocolUtil.BytesToString(source));

                HandleRequestUserInfo(ref response, frameBean);
                break;

            case MsgId.X0007:    //请求照片数据OK
                logger.Info("收到请求照片数据OK:" + ProtocolUtil.BytesToString(source));
                WriteLogFile("收到请求照片数据OK:" + ProtocolUtil.BytesToString(source));
                HandleRequestImageData(ref response, frameBean);
                break;

            default:
                logger.Error("收到未知消息:" + ProtocolUtil.BytesToString(source));
                WriteLogFile("收到未知消息:" + ProtocolUtil.BytesToString(source));
                frameBean.AppendErrorMsg("未知的消息ID");
                byte[] d = MakerTCPFrame.GetInstance().Make8001Frame(serialNo, MsgId.X0001, CommResponse.UnSupport);
                response = MakerTCPFrame.GetInstance().PackData(MsgId.X8001, frameBean.TerminalId, d);
                break;
            }
            logger.Info("响应的报文:" + ProtocolUtil.BytesToString(response));
            WriteLogFile("响应的报文:" + ProtocolUtil.BytesToString(response));
            return(response);
        }
        /// <summary>
        /// 接收数据的监听方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnPortDataReceived(Object sender, SerialDataReceivedEventArgs e)
        {
            try
            {
                Thread.Sleep(50);
                isReceive = true;//收到数据,取消重发

                byte[] buffer = null;;
                int    len    = serialPort.BytesToRead;

                byte[] receiveData = new byte[len];
                serialPort.Read(receiveData, 0, len);
                //Console.WriteLine("收到的数据:" + ProtocolUtil.ByteToStringOk(receiveData));
                int offset = 0;

                Int32 datalen = 0;
                if (len > 0 && receiveData[0] == 0xAA)//第一包数据
                {
                    datalen = Convert.ToInt32((receiveData[2].ToString("X2") + receiveData[3].ToString("X2")), 16);
                    //Console.WriteLine("数据的长度:" + datalen);
                    buffer = new byte[datalen + 6];

                    for (int i = 0; i < receiveData.Length; i++)
                    {
                        buffer[i] = receiveData[i];
                    }
                    offset = receiveData.Length;
                }
                else
                {
                    return;
                }


                while (buffer != null && buffer[buffer.Length - 1] != 0xCC)
                {
                    Thread.Sleep(50);
                    int len2 = serialPort.BytesToRead;

                    if (len2 <= 0)
                    {
                        return;
                    }

                    serialPort.Read(buffer, offset, len2);
                    offset += len2;

                    if (offset > buffer.Length)
                    {
                        return;
                    }
                }

                //下面是完整数据
                if (buffer != null)
                {
                    byte[] data = new byte[datalen + 3];
                    Array.Copy(buffer, 1, data, 0, data.Length);
                    if (buffer[buffer.Length - 2] == ProtocolUtil.XorByByte(data))
                    {
                        object result = null;//用于存放uuid的鉴权加密
                        new ParserUSBDogFrame().Parser(ref result, buffer);
                        string b = ProtocolUtil.BytesToString((byte[])result);
                        //Console.WriteLine("解密通讯加密后的数据:" + b);

                        byte[] uuidBytes = null;
                        uuidBytes = AesUtil.Decrypt((byte[])result, ProtocolConstant.USB_DOG_AUTH_PASSWORD);
                        //Console.WriteLine("解密鉴权加密后的数据:" + ProtocolUtil.ByteToStringOk(uuidBytes));

                        string strUUID = System.Text.Encoding.ASCII.GetString(uuidBytes);

                        if (strUUID == Get_UUID())
                        {
                            //MessageBoxX.Info("激活成功");
                            //todo 全局变量
                            ProtocolConstant.USB_SUCCESS = 1;
                            MessageBoxX.Info(LanguageUtils.ConvertLanguage("激活成功", "Activated successfully"));

                            //Console.WriteLine("激活成功");
                            logger.Debug("激活成功");
                        }
                        else
                        {
                            //MessageBox.Show("激活失败");
                            //Console.WriteLine("激活失败");
                            logger.Debug("激活失败");
                        }
                    }
                    else
                    {
                        //Console.WriteLine("校验失败");
                        logger.Debug("校验失败");
                    }
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                //收到消息后至空串口并关闭
                SerialPortUtil.ClosePort(ref serialPort);
                Dispatcher.Invoke(new Action(() =>
                {
                    this.Close();
                }));
            }
        }