예제 #1
0
        public void EncryptAndDecryptTest()
        {
            string           plainText   = "hello,my name is jsjs-gchao";
            AsymCryptography cryptServer = new AsymCryptography();
            AsymCryptography cryptClient = new AsymCryptography();

            cryptClient.PublicKey = cryptServer.PublicKey;

            string cryptedText = cryptClient.Encrypt(plainText);

            string decryptedText = cryptServer.Decrypt(cryptedText);

            Assert.AreEqual(plainText, decryptedText, decryptedText);
        }
예제 #2
0
        private void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                m_hostIP = txtIP.Text;
                IPAddress ipA = IPAddress.Parse(m_hostIP);
                m_port     = int.Parse(txtPort.Text.Trim());
                m_endPoint = new IPEndPoint(ipA, m_port);

                m_client = new UdpClient();
                m_client.Connect(m_endPoint);

                m_sendData = "ADD";
                m_data     = UDPComm.UDPComm.EncodingASCII(m_sendData);
                m_client.Send(m_data, m_data.Length);

                string content = string.Empty;

                if (ReceiveData(UDPComm.ConstValue.PUBLIC_KEY, ref content))
                {
                    m_asymCrpt           = new AsymCryptography();
                    m_asymCrpt.PublicKey = content;
                    content = m_asymCrpt.Encrypt(m_symCrpt.Key);
                    content = string.Format("{0}{1}", UDPComm.ConstValue.SYM_KEY, content);

                    m_data = UDPComm.UDPComm.EncodingASCII(content);
                    m_client.Send(m_data, m_data.Length);

                    if (ReceiveData(UDPComm.ConstValue.OK, ref content))
                    {
                        string time = DateTime.Now.ToString("t");
                        txtContent.Text = time + "  " + "Add sucessfully\r\n" + txtContent.Text;
                    }
                }

                if (m_client != null)
                {
                    m_receiveThread = new Thread(new ThreadStart(Listener));
                    m_receiveThread.Start();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }