コード例 #1
0
        /// <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);
        }
コード例 #2
0
        /// <summary>
        /// 加密并发送信息
        /// </summary>
        /// <param name="RSAPublicKey">获得的公钥</param>
        /// <param name="content">需加密的内容</param>
        /// <returns>加密后的内容</returns>
        private byte[] Encrypt(Socket socketSend, string RSAPublicKey, string content)
        {
            ///加密
            RSAChange rsa        = new RSAChange();
            string    encryptStr = rsa.RSAEncrypt(RSAPublicKey, content); //加密

            byte[] buffer = Encoding.UTF8.GetBytes(encryptStr);           //转换成可以传输的类型

            ///发送类型
            string type = "ND";

            byte[] typeB = Encoding.UTF8.GetBytes(type);

            ///发送
            socketSend.Send(typeB);  //发送类型
            socketSend.Send(buffer); //发送加密后结果

            return(buffer);
        }