Exemplo n.º 1
0
    public int encrypt(byte[] raw, int offset, int size)
    {
        // reserve checksum
        size += 4;

        if (_static)

        {
            // reserve for XOR "key"
            size += 4;

            // padding
            size += 8 - (size % 8);
            NewCrypt.encXORPass(raw, offset, size, rnd.Next());
            _staticCrypt.crypt(raw, offset, size);

            _static = false;
        }
        else

        {
            // padding
            size += 8 - (size % 8);
            NewCrypt.appendChecksum(raw, offset, size);
            _crypt.crypt(raw, offset, size);
        }
        return(size);
    }
Exemplo n.º 2
0
        /// <summary>
        /// 数据写入
        /// </summary>
        /// <param name="buffer">数据缓存</param>
        /// <param name="offset">从字节偏移处拷贝</param>
        /// <param name="count">写入的长度</param>
        public bool FinalWriteData(byte[] buffer, int offset, int count)
        {
            if (null != PacketBytes)
            {
                //LogManager.WriteLog(LogTypes.Error, string.Format("TCP发出命令包不能被重复写入数据, 命令ID: {0}", PacketCmdID));
                return(false);
            }

            int PKT_SIZE = 6 + count;

            if (GameManager.IsTcpCrypt)
            {
                PKT_SIZE = 6 + NewCrypt.getAfterPaddingLength(count);
            }
            //先判断是否超出的最大包的大小
            if ((PKT_SIZE) >= (int)TCPCmdPacketSize.MAX_SIZE)
            {
                //throw new Exception(string.Format("TCP命令包长度最大不能超过: {0}", (int)TCPCmdPacketSize.MAX_SIZE));
                //LogManager.WriteLog(LogTypes.Error, string.Format("TCP命令包长度:{0}, 最大不能超过: {1}, 命令ID: {2}", count, (int)TCPCmdPacketSize.MAX_SIZE, PacketCmdID));
                return(false);
            }

            /////encrypt



            //理论上这儿是不会返回NULL的,如果返回,直接崩溃,外部会接收到异常
            //if (GameManager.FlagOptimizeThreadPool2)
            //{
            //    _MemoryBlock = TMSKThreadStaticClass.GetInstance().PopMemoryBlock(PKT_SIZE);
            //}
            //else
            //{
            //    _MemoryBlock = Global._MemoryManager.Pop(PKT_SIZE);
            //}
            _MemoryBlock = new MemoryBlock(PKT_SIZE, false);
            PacketBytes  = _MemoryBlock.Buffer;

            //写入数据
            int offsetTo = (int)6;

            DataHelper.CopyBytes(PacketBytes, offsetTo, buffer, offset, count);
            _PacketDataSize = count;
            Final();

            return(true);
        }
Exemplo n.º 3
0
 public bool decrypt(byte[] raw, int offset, int size)
 {
     _crypt.decrypt(raw, offset, size);
     return(NewCrypt.verifyChecksum(raw, offset, size));
 }
Exemplo n.º 4
0
 public void setKey(byte[] key)
 {
     _crypt = new NewCrypt(key);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Initializes new instance of <see cref="UserConnection"/> class.
 /// </summary>
 /// <param name="socket">Connection <see cref="Socket"/>.</param>
 internal UserConnection(Socket socket)
     : base(socket)
 {
     Session = InitializeSession((( IPEndPoint )socket.RemoteEndPoint).Address.ToString());
     m_Crypt = new NewCrypt(Session.BlowfishKey);
 }
Exemplo n.º 6
0
 public static void appendChecksum(byte[] raw)
 {
     NewCrypt.appendChecksum(raw, 0, raw.Length);
 }
Exemplo n.º 7
0
 public static bool verifyChecksum(byte[] raw)
 {
     return(NewCrypt.verifyChecksum(raw, 0, raw.Length));
 }
Exemplo n.º 8
0
 /**
  * Packet is first XOR encoded with <code>key</code> Then, the last 4 bytes are overwritten with the the XOR "key". Thus this assume that there is enough room for the key to fit without overwriting data.
  * @param raw The raw bytes to be encrypted
  * @param key The 4 bytes (int) XOR key
  */
 public static void encXORPass(byte[] raw, int key)
 {
     NewCrypt.encXORPass(raw, 0, raw.Length, key);
 }