Пример #1
0
 /// <summary>
 /// Creates a new instance of the ShardSession class.
 /// </summary>
 public ShardSession() : base(SessionStatus.Connected)
 {
     commandQueue = new ConcurrentQueue <CommandClosure>();
     Cipher       = new PacketCipher();
     AccountID    = -1;
     Awareness    = new HashSet <GameObject>();
 }
Пример #2
0
        /// <summary>
        /// Test the PacketCipher class implementation
        /// <para>Throws an Exception on failure</</para>
        /// </summary>
        public static void PacketCipherTest()
        {
            const int BLSZ = 1024;
            KeyParams key;

            byte[]       data;
            MemoryStream instrm;
            MemoryStream outstrm = new MemoryStream();

            using (KeyGenerator kg = new KeyGenerator())
            {
                // get the key
                key = kg.GetKeyParams(32, 16);
                // 2 * 1200 byte packets
                data = kg.GetBytes(BLSZ * 2);
            }
            // data to encrypt
            instrm = new MemoryStream(data);

            // Encrypt a stream //
            // create the outbound cipher
            using (ICipherMode cipher = new CTR(new RHX()))
            {
                // initialize the cipher for encryption
                cipher.Initialize(true, key);
                // set block size
                ((CTR)cipher).ParallelBlockSize = BLSZ;

                // encrypt the stream
                using (PacketCipher pc = new PacketCipher(cipher))
                {
                    byte[] inbuffer  = new byte[BLSZ];
                    byte[] outbuffer = new byte[BLSZ];
                    int    bytesread = 0;

                    while ((bytesread = instrm.Read(inbuffer, 0, BLSZ)) > 0)
                    {
                        // encrypt the buffer
                        pc.Write(inbuffer, 0, outbuffer, 0, BLSZ);
                        // add it to the output stream
                        outstrm.Write(outbuffer, 0, outbuffer.Length);
                    }
                }
            }

            // reset stream position
            outstrm.Seek(0, SeekOrigin.Begin);
            MemoryStream tmpstrm = new MemoryStream();

            // Decrypt a stream //
            // create the inbound cipher
            using (ICipherMode cipher = new CTR(new RHX()))
            {
                // initialize the cipher for decryption
                cipher.Initialize(false, key);
                // set block size
                ((CTR)cipher).ParallelBlockSize = BLSZ;

                // decrypt the stream
                using (PacketCipher pc = new PacketCipher(cipher))
                {
                    byte[] inbuffer  = new byte[BLSZ];
                    byte[] outbuffer = new byte[BLSZ];
                    int    bytesread = 0;

                    while ((bytesread = outstrm.Read(inbuffer, 0, BLSZ)) > 0)
                    {
                        // process the encrypted bytes
                        pc.Write(inbuffer, 0, outbuffer, 0, BLSZ);
                        // write to stream
                        tmpstrm.Write(outbuffer, 0, outbuffer.Length);
                    }
                }
            }

            // compare decrypted output with data
            if (!Evaluate.AreEqual(tmpstrm.ToArray(), data))
            {
                throw new Exception();
            }
        }
Пример #3
0
 // static Option<Packet> ReadPacket(ByteStringReader reader,bool encrypted = false) {
 //   var l = reader.ReadShortLE();
 //   var bytes = reader.ReadBytes(l);
 //   var crc = reader.ReadShortLE();
 //
 //   if (crc == _calcCrc(bytes))
 //     return Option<Packet>.Some(new Packet(byets,encrypted));
 //   return Option<Packet>.None;
 // }
 public Packet Encode(PacketCipher cipher)
 {
     if (_encrypted) {
     return this;
       } else {
     var b = ToByteString();
     return new Packet(cipher.Encrypt(b),true);
       }
 }
Пример #4
0
        public Option<Packet> Decode(PacketCipher cipher)
        {
            if (!_encrypted) {
            return Option<Packet>.Some(this);
              } else {
            try {
              var b = cipher.Decrypt(_bytes);
              var src = new ByteStringReader(b);
              var l = (int)(new VLong(src).V);
              var enc = false;
              if (l < 0) {
            enc = true;
            l = -l;
              }

              if (l + 2 > src.Length) {
            throw new SystemException("Packet corrupted");
              }

              var pkt = new Packet(new ByteString(src.ReadBytes(l)),enc);
              var crc = src.ReadShortLE();

              if (crc != pkt.Crc) {
            throw new SystemException("Invalid CRC");
              }
              return Option<Packet>.Some(pkt);
            } catch(Exception e) {
              return Option<Packet>.None;
            }
              }
        }
Пример #5
0
 public ShardComposer(IWhisperRequestFactory <ShardRequest, ShardClientOpcode> requestFactory, IDictionary <string, IWhisperCommand <ShardSession, ShardRequest> > commandDictionary, PacketCipher cipher) : base(requestFactory, commandDictionary)
 {
     this.cipher = cipher;
 }