예제 #1
0
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="packageInfo">消息包</param>
        /// <param name="buffer">数据</param>
        /// <param name="structure">结构</param>
        /// <param name="offset">偏移量</param>
        /// <returns></returns>
        public override void Decrypt(IJTTPackageInfo packageInfo, ReadOnlySpan <byte> buffer, StructureInfo structure, int offset)
        {
            try
            {
                if (protocol.Encrypt.Targets?.ContainsKey(structure.Id) != true)
                {
                    return;
                }

                var encryptProperty = protocol.Encrypt.Targets[structure.Id];
                var flag            = (bool)packageInfo.GetPropertyValue(encryptProperty.Flag.Split('.'));
                if (!flag)
                {
                    return;
                }

                var length = buffer.Length - offset - protocol.Structures.Where(o => o.Order > structure.Order).Sum(o => o.Length.Value);
                var key    = (UInt32)packageInfo.GetPropertyValue(encryptProperty.Key.Split('.'));
                using MemoryStream ms_encrypt = new MemoryStream();
                var writer = new BinaryWriter(ms_encrypt);
                using (MemoryStream ms = new MemoryStream(buffer.ToArray()))
                {
                    var reader = new BinaryReader(ms);

                    //获取加密的数据
                    ms.Seek(offset, SeekOrigin.Begin);

                    //解密
                    while (ms.Position < length)
                    {
                        var Char = reader.ReadChar();

                        //将传输的数据与伪随机码按字节进行异或运算
                        key   = protocol.Encrypt.IA1 * (key % protocol.Encrypt.M1) + protocol.Encrypt.IC1;
                        Char ^= (Char)((key << 20) & 0xff);

                        //写入解密的数据
                        writer.Write(Char);
                    }
                }
                buffer = ms_encrypt.ToArray();
            }
            catch (Exception ex)
            {
                throw new JTTException($"解密时发生错误, structureId: {structure.Id}.", ex);
            }
        }
예제 #2
0
        public override void Encrypt(IJTTPackageInfo packageInfo, byte[] buffer, StructureInfo structure)
        {
            try
            {
                if (protocol.Encrypt.Targets?.ContainsKey(structure.Id) != true)
                {
                    return;
                }

                var encryptProperty = protocol.Encrypt.Targets[structure.Id];
                if (!(bool)packageInfo.GetPropertyValue(encryptProperty.Flag.Split('.')))
                {
                    return;
                }

                //生成Key
                var key_bytes = new byte[4];
                new Random().NextBytes(key_bytes);
                var key = BitConverter.ToUInt32(key_bytes);

                //写入Key
                packageInfo.SetValueToProperty(encryptProperty.Key.Split('.'), key);

                using MemoryStream ms_encrypt = new MemoryStream();
                var writer = new BinaryWriter(ms_encrypt);
                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    var reader = new BinaryReader(ms);

                    while (ms.Position < ms.Length)
                    {
                        var @char = reader.ReadChar();

                        //将待传输的数据与伪随机码按字节进行异或运算
                        key    = protocol.Encrypt.IA1 * (key % protocol.Encrypt.M1) + protocol.Encrypt.IC1;
                        @char ^= (Char)((key >> 20) & 0xff);

                        //写入加密的数据
                        writer.Write(@char);
                    }
                }
                buffer = ms_encrypt.ToArray();
            }
            catch (Exception ex)
            {
                throw new JTTException($"加密时发生错误, structureId: {structure.Id}.", ex);
            }
        }