Ejemplo n.º 1
0
        public void ClientSendClient()
        {
            //输入发送的消息
            Console.WriteLine("Send {0} Message:", clientEndPoint.ToString());
            //string sendMsg = Console.ReadLine();
            //1.准备要签名的原文消息
            string sendMsg = "China";

            //对消息进行二进制编码
            byte[] plainData = Encoding.UTF8.GetBytes(sendMsg);
            //打印消息
            Console.WriteLine("PlainText:" + sendMsg);
            //DES加密
            //设置加密密钥
            Console.WriteLine("Please Input Key");
            //string KEY_64 = Console.ReadLine();
            string KEY_64 = "A4G-8=Jk";

            Console.WriteLine(KEY_64);
            //设置初始化向量
            Console.WriteLine("PLease INput IV");
            //string IV_64 = Console.ReadLine();
            string IV_64 = "JKbN=5[?";

            Console.WriteLine(IV_64);
            byte[] KeyBytes = Encoding.UTF8.GetBytes(KEY_64);
            byte[] IVBytes  = Encoding.UTF8.GetBytes(IV_64);


            //Console.WriteLine();
            //用客户端私钥数据进行签名
            csData.rsaSignData = RSA_C_S.RsaSignData(plainData, rsaPrivatePara);
            //对消息进行加密
            csData.dasEncryptData = DES_C_C.TransformBuffer(plainData, "Encrypt", KeyBytes, IVBytes);
            //Console.WriteLine("UDP Server DES Data*****: {0}", BitConverter.ToString(plainData));
            //Console.WriteLine("UDP Client Sign Data*****: {0}",BitConverter.ToString(csData.rsaSignData));
            //Console.WriteLine("UDP Server DES Data*****: {0}", BitConverter.ToString(csData.dasEncryptData));
            //Console.WriteLine("UDP Client DES Data*****: {0}", BitConverter.ToString(csData.clientRSAPublicPar.Exponent));
            // Console.WriteLine("UDP Client DES Data*****: {0}", BitConverter.ToString(csData.clientRSAPublicPar.Modulus));
            //csData序列化
            MemoryStream    mStream    = new MemoryStream();
            BinaryFormatter bFormatter = new BinaryFormatter();

            bFormatter.Serialize(mStream, csData);
            mStream.Flush();
            mStream.Position = 0;
            //发送对象
            try
            {
                udpClient.Send(mStream.GetBuffer(), (int)mStream.Length, clientEndPoint);
            }
            catch (Exception ex)
            {
                Console.WriteLine("Send Error!!! | Remote Client isn't Open Listening");
                Console.WriteLine(ex.ToString());
            }
            Console.WriteLine("{0} to {1} Sucess", udpClient.Client.LocalEndPoint.ToString(), clientEndPoint.ToString());
        }
Ejemplo n.º 2
0
        //private static RSAUserPrivateparam rsaPrivateParams=new RSAUserPrivateparam();
        public void TCP_Client()
        {
            c_s.commands = "SendCleint";
            byte[] plainData = Encoding.UTF8.GetBytes("China");
            //创建RSACryptoServiceProvider实例,以自动生成公钥对并保存之
            RSACryptoServiceProvider rsaProvider = new RSACryptoServiceProvider();

            //set Init Public Key
            c_s.clientRSAPublicPar = rsaProvider.ExportParameters(false);
            //导出参数信息,包含私钥
            rsaPrivateParameter = rsaProvider.ExportParameters(true);
            //
            rsaProvider.Clear();
            //使用私钥对消息进行签名   保存签名
            c_s.rsaSignData = RSA_C_S.RsaSignData(plainData, rsaPrivateParameter);
            while (true)
            {
                Console.WriteLine(" 0 Create TCP Client & Connect Server.");
                Console.WriteLine(" 1 Put Key To Server");
                Console.WriteLine(" 2 Get Client List.");
                Console.WriteLine(" 3 Talk To Another Client.");
                Console.WriteLine(" 4 Exit!");
                int number = Convert.ToInt32(Console.ReadLine());
                switch (number)
                {
                case 0:
                    ConnectServer();
                    break;

                case 1:
                    SendPublicKey();
                    break;

                case 2:
                    GetClientList();
                    break;

                case 3:
                    ClientToClient();
                    break;

                case 4:
                    ClientClose();
                    return;
                }
            }
        }