func (e *Encryption) Encrypt(data []byte) ([]byte, error) {
	var iLen = len(data)
	if iLen < S2CHeaderSize {
		return nil, errors.New(
			fmt.Sprintf(
				"Packet length is smaller than required! Detected: %d, required: %d",
				iLen, S2CHeaderSize,
			),
		)
	}
	var pPayload = make([]byte, iLen)
	var xorKey uint32
	var xorNum uint32
	xorNum = binary.LittleEndian.Uint32(data) ^ SendXorKey
	binary.LittleEndian.PutUint32(pPayload, xorNum)
	xorKey = e.Key.KeyTable[xorNum&RecvXorKeyNumMask]
	var payloadLen = iLen - (S2CHeaderSize - MainCMDSize)
	var j int32 = 4
	for i := 0; i < payloadLen/4; i++ {
		xorNum = binary.LittleEndian.Uint32(data[j:j+4]) ^ xorKey
		binary.LittleEndian.PutUint32(pPayload[j:j+4], xorNum)
		xorKey = e.Key.KeyTable[xorNum&RecvXorKeyNumMask]
		j += 4
	}
	var remainLen = int32(iLen % 4)
	var remainData = make([]byte, 4)
	copy(remainData, data[j:j+remainLen])
	var r = binary.LittleEndian.Uint32(remainData)
	var m = mask[remainLen]
	xorNum = r ^ xorKey
	r = xorNum ^ (xorKey & m)
	binary.LittleEndian.PutUint32(remainData, r)
	copy(pPayload[j:j+remainLen], remainData[0:remainLen])
	return pPayload, nil
}
Attempts to encrypt and return data. An error is returned on fail
package encryption
import (
	"encoding/binary"
	"errors"
	"fmt"
	"math/rand"
)
var mask = []uint32{0xFFFFFFFF, 0xFFFFFF00, 0xFFFF0000, 0xFF000000}
type Encryption struct {
	recvXorKey              uint32
	isFirstPacket           bool
	xorKeyTableBaseMultiple uint32
	Key           *XorKeyTable
	RecvXorKeyIdx uint32
}
func (e *Encryption) Init(key *XorKeyTable) {
	e.isFirstPacket = true
	e.xorKeyTableBaseMultiple = 1
	e.Key = key
	e.RecvXorKeyIdx = uint32(rand.Intn(RecvXorKeyNum))
}
func (e *Encryption) GetPacketSize(data []byte) int {
	if !e.isFirstPacket {
		var header = binary.LittleEndian.Uint32(data) ^ e.recvXorKey
		return int(header >> 16)
	}
	return Connect2SvrSize
}