/// <summary> /// 接收到需要返回解密的处理方式 /// </summary> /// <returns>解密后的内容</returns> private string ReciveNeedDecrypt(Socket socketSend) { RSAKey rk = new RSAKey(); RSAChange rDecrypt = new RSAChange(); ///接收 byte[] buffer = new byte[1024 * 5]; //一次为5k int r = socketSend.Receive(buffer); //接收 string str = Encoding.UTF8.GetString(buffer, 0, r); //转成字符串 ///解密 string strDecrypt = rDecrypt.RSADecrypt(rk.getRSAKey(@"privateKey.xml"), str);//解密 return(strDecrypt); }
/// <summary> /// 接收到需要返回公钥的处理方式 /// </summary> /// <param name="socketSend">需回复的ip</param> private void ReciveNeedPublicKey(Socket socketSend) { RSAKey rk = new RSAKey(); rk.setRSAKey(@"privateKey.xml", @"publicKey.xml");//创建公钥私钥,并写入指定文件夹内 ///序号 string typeStr = "PK"; byte[] numByte = Encoding.UTF8.GetBytes(typeStr); ///公钥 string keyStr = rk.getRSAKey(@"publicKey.xml"); byte[] keyByte = Encoding.UTF8.GetBytes(keyStr); ///发送 socketSend.Send(numByte, 2, 0); //发送序号 socketSend.Send(keyByte, keyByte.Length, 0); //发送公钥 }