Esempio n. 1
0
        public void Connect(string server, int port = 13000)
        {
            try
            {
                Client = new TcpClient(server, port);


                Stream = Client.GetStream();

                //Client.ReceiveBufferSize = 10_048_576;
                //Client.SendBufferSize = 10_048_576;

                Task.Run(ListenToServer);
                Task.Run(ProcessIO);
                RSAParameters param;
                using (var rsa = new RSACryptoServiceProvider(512))
                {
                    param          = rsa.ExportParameters(false);
                    this.RSAParams = rsa.ExportParameters(true);
                }
                using (var aes = new AesCryptoServiceProvider())
                {
                    this.PhysicalKey = new AESKey {
                        IV = aes.IV, Key = aes.Key
                    };
                }
                this.SessionKey = Helper.GenerateSessionKey();
                string encSession = Helper.AESEncrypt(JsonConvert.SerializeObject(this.SessionKey), this.PhysicalKey);
                this.SendMessage($"init{Helper.SocketMessageAttributeSeperator}{Name}{Helper.SocketMessageAttributeSeperator}{JsonConvert.SerializeObject(param)}{Helper.SocketMessageAttributeSeperator}{JsonConvert.SerializeObject(PhysicalKey)}{Helper.SocketMessageAttributeSeperator}{encSession}", EncryptionMode.RSA);
            }
            catch (Exception)
            {
            }
        }
Esempio n. 2
0
        public void Send(string msg, EncryptionMode mode)
        {
            string encryptionIndicator = mode switch
            {
                EncryptionMode.AES => "aes",
                EncryptionMode.None => "non",
                EncryptionMode.RSA => "rsa",
                _ => ""
            } +Helper.SocketMessageAttributeSeperator;

            if (mode == EncryptionMode.RSA)
            {
                msg += Helper.MacSeperator + Helper.RSASign(msg, TcpListener.Instance.RSAPrivateParameters);
                msg  = Helper.RSAEncrypt(msg, this.RSAParameters);
            }
            else if (mode == EncryptionMode.AES)
            {
                msg += Helper.MacSeperator + Helper.RSASign(msg, TcpListener.Instance.RSAPrivateParameters);
                var buffer  = new StringBuilder();
                var maxSize = 100;
                int iters   = (int)Math.Ceiling(1f * msg.Length / maxSize);

                for (var i = 0; i < iters; i++)
                {
                    string item;
                    if (i * maxSize + 100 > msg.Length)
                    {
                        item = msg.Substring(i * maxSize);
                    }
                    else
                    {
                        item = msg.Substring(i * maxSize, 100);
                    }
                    if (this.SessionKey.IsExpired)
                    {
                        buffer.Append(Helper.SessionKeySeperator);
                        this.SessionKey = Helper.GenerateSessionKey();
                        buffer.Append(Helper.AESEncrypt(JsonConvert.SerializeObject(this.SessionKey), this.PhysicalKey));
                        buffer.Append(Helper.SessionKeySeperator);
                        buffer.Append(Helper.AESChunkSeperator);
                    }
                    buffer.Append(Helper.AESEncrypt(item, this.SessionKey.AESKey));
                    buffer.Append(Helper.AESChunkSeperator);
                }

                msg = buffer.ToString();
            }
            this.SendQueue.Enqueue(encryptionIndicator + msg + Helper.SocketMessageSplitter);
        }
Esempio n. 3
0
        /// <summary>
        /// body AES加密
        /// </summary>
        /// <param name="body"></param>
        /// <returns></returns>
        public static string BodyForAesEncrypt(object body)
        {
            var bodyData = JsonSerializeHelper.ToJsonForlowercase(body);

            return(Helper.EncodeBytes(Helper.AESEncrypt(bodyData, CtripConfig.AesKey, CtripConfig.AesIv)));
        }
Esempio n. 4
0
        public async void SendMessage(string msg, EncryptionMode mode)
        {
            while (!Initialized)
            {
                await Task.Delay(100);
            }
            string encryptionIndicator = "";

            switch (mode)
            {
            case EncryptionMode.None:
                encryptionIndicator = "non";
                break;

            case EncryptionMode.RSA:
                encryptionIndicator = "rsa";
                break;

            case EncryptionMode.AES:
                encryptionIndicator = "aes";
                break;

            default:
                break;
            }
            encryptionIndicator += Helper.SocketMessageAttributeSeperator;

            if (mode == EncryptionMode.RSA)
            {
                msg += Helper.MacSeperator + Helper.RSASign(msg, this.RSAParams);
                msg  = Helper.RSAEncrypt(msg, this.ServerParams);
            }
            if (mode == EncryptionMode.AES)
            {
                msg += Helper.MacSeperator + Helper.RSASign(msg, this.RSAParams);
                var buffer  = new StringBuilder();
                var maxSize = 100;
                int iters   = (int)Math.Ceiling(1f * msg.Length / maxSize);

                for (var i = 0; i < iters; i++)
                {
                    string item;
                    if (i * maxSize + 100 > msg.Length)
                    {
                        item = msg.Substring(i * maxSize);
                    }
                    else
                    {
                        item = msg.Substring(i * maxSize, 100);
                    }
                    if (this.SessionKey.IsExpired)
                    {
                        buffer.Append(Helper.SessionKeySeperator);
                        this.SessionKey = Helper.GenerateSessionKey();
                        buffer.Append(Helper.AESEncrypt(JsonConvert.SerializeObject(this.SessionKey), this.PhysicalKey));
                        buffer.Append(Helper.SessionKeySeperator);
                        buffer.Append(Helper.AESChunkSeperator);
                    }
                    buffer.Append(Helper.AESEncrypt(item, this.SessionKey.AESKey));
                    buffer.Append(Helper.AESChunkSeperator);
                }
                msg = buffer.ToString();
            }

            this.SendQueue.Enqueue(encryptionIndicator + msg + Helper.SocketMessageSplitter);
        }