示例#1
0
        public static string Decrypt(string Data, byte[] Key, byte[] IV)
        {
            try
            {
                MemoryStream msDecrypt = new MemoryStream(Convert.FromBase64String(Data));

                // Create a CryptoStream using the MemoryStream
                // and the passed key and initialization vector (IV).
                CryptoStream csDecrypt = new CryptoStream(msDecrypt,
                                                          new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV),
                                                          CryptoStreamMode.Read);

                // Create buffer to hold the decrypted data.
                byte[] fromEncrypt = new byte[Data.Length];

                // Read the decrypted data out of the crypto stream
                // and place it into the temporary buffer.
                csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

                //Convert the buffer into a string and return it.
                string ReturnValue = new ASCIIEncoding().GetString(fromEncrypt);
                if (ReturnValue.Contains("\0\0\0"))
                {
                    ReturnValue = ReturnValue.Remove(ReturnValue.IndexOf("\0\0\0"));
                }
                return(ReturnValue);
            }
            catch (CryptographicException e)
            {
                Console.WriteLine("A Cryptographic error occurred: {0}", e.Message);
                return(null);
            }
        }
示例#2
0
        void s_DataReceived(object sender, SerialDataReceivedEventArgs e)   //数据接收事件,
        {
            //Thread.Sleep(200); //等待100毫秒
            int count = serialPart.BytesToRead;

            byte[] buff        = new byte[count];
            string respond_str = null;

            serialPart.Read(buff, 0, count);
            //1.缓存数据
            if (send_state == 3)//导出记录
            {
                //this.AddData(Encoding.Default.GetBytes("."));//输出数据
                save_timer.Stop();
                record_buffer.AddRange(buff);
                save_timer.Start();
            }
            else
            {
                buffer.AddRange(buff);
            }
            if (buffer.Count > 0) //至少包含帧头(2字节)、长度(1字节)、校验位(1字节);根据设计不同而不同
            {
                byte[] ReceiveBytes = new byte[buffer.Count];
                //得到完整的数据,复制到ReceiveBytes中进行校验
                buffer.CopyTo(0, ReceiveBytes, 0, buffer.Count);

                respond_str = new ASCIIEncoding().GetString(ReceiveBytes);
                if (respond_str == "\b")
                {
                    return;
                }
                if (send_state == 0)
                {
                    if (respond_str.Length > 0)
                    {
                        respond_str = respond_str.Remove(respond_str.Length - 1);
                    }
                    if (respond_str == "\b \b\b:")
                    {
                    }
                    else
                    {
                        if (respond_str == "\b \b\b")
                        {
                            respond_str = richTextBox1.Text.Remove(richTextBox1.Text.Length - 1);
                            richTextBox1.Clear();
                            richTextBox1.AppendText(respond_str);
                        }
                        else
                        {
                            this.AddData(ReceiveBytes);//输出数据
                        }
                        if (respond_str == "The app was not valid!!!\r")
                        {
                            //respond_str = " ";
                            download_process();
                        }
                        if (respond_str == "write iap_code_buf to nand flash block 10 page 0 nand addr 0 completed\r\n\r\nNick-Cmd")
                        {
                            send_str("reset\r");
                        }
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }
                else if (send_state == 1)//发送代码状态
                {
                    if (respond_str == "OK\r\n")
                    {
                        buffer.RemoveRange(0, buffer.Count);
                        Interlocked.Exchange(ref respond_msg, 0xA001);
                    }
                    else if (respond_str == "write start\r\n\r\nNick-Cmd:")/////执行其他代码,对数据进行处理。
                    {
                        buffer.RemoveRange(0, buffer.Count);
                        Interlocked.Exchange(ref respond_msg, 0xA001);
                    }
                    else if (respond_str == "ERROR\r\n")/////执行其他代码,对数据进行处理。
                    {
                        buffer.RemoveRange(0, buffer.Count);
                        Interlocked.Exchange(ref respond_msg, 0xE001);
                    }
                }
                else
                {
                    if (respond_str.Length > 1)
                    {
                        if ((respond_str[respond_str.Length - 2] == '\r') && (respond_str[respond_str.Length - 1] == '\n'))
                        {
                            coinOp.CoinOP_SetText(respond_str);
                            buffer.RemoveRange(0, buffer.Count);
                        }
                    }
                }
            }
        }