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()); }
private static void UDPReceiveServer(object client) { UdpClient udpServer = client as UdpClient; Console.WriteLine("UDP Server Enable........"); while (true) { //Console.WriteLine("等待接收数据"); IPEndPoint clientEndPoint = null; try { buffer = udpServer.Receive(ref clientEndPoint); }catch (Exception ex) { Console.WriteLine("Client Closeing"); } //对buffer数据进行认证和 MemoryStream mStream = new MemoryStream(); BinaryFormatter bFormatter = new BinaryFormatter(); mStream.Write(buffer, 0, buffer.Length); mStream.Flush(); mStream.Position = 0; ClientServer cs = bFormatter.Deserialize(mStream) as ClientServer; //解密 //设置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); //先DES解密 byte[] plainData = DES_C_C.TransformBuffer(cs.dasEncryptData, "Decrypt", KeyBytes, IVBytes); string receiveMsg = Encoding.UTF8.GetString(plainData); //验证签名 //Console.WriteLine("UDP Server DES Data*****: {0}", BitConverter.ToString(plainData)); //Console.WriteLine("UDP Server Sign Data*****: {0}", BitConverter.ToString(cs.rsaSignData)); //Console.WriteLine("UDP Server DES Data*****: {0}", BitConverter.ToString(cs.dasEncryptData)); //Console.WriteLine("UDP Client DES Data*****: {0}", BitConverter.ToString(cs.clientRSAPublicPar.Exponent)); //Console.WriteLine("UDP Client DES Data*****: {0}", BitConverter.ToString(cs.clientRSAPublicPar.Modulus)); //对数据进行验证签名 bool isRight = RSA_C_S.RsaVerifyData(plainData, cs.clientRSAPublicPar, cs.rsaSignData); string VerifyResult = isRight ? "YES" : "NO"; Console.WriteLine("Server Message****Verify Result :{0}", VerifyResult); Console.WriteLine("Server Message****Receive From {0} Message:{1} ", clientEndPoint.ToString(), receiveMsg); } }
//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; } } }