public static Packet[] ReadAllPackets(byte[] data) { List <Packet> packets = new List <Packet>(); using (MemoryStream ms = new MemoryStream(data)) { using (BinaryReader br = new BinaryReader(ms)) { try { while (br.BaseStream.Position != br.BaseStream.Length) { Packet temp = new Packet(); temp.Size = br.ReadInt16(); temp.pub_key = br.ReadByte(); byte[] content = br.ReadBytes(temp.Size); byte pub_keyindex = XCRYPT.GetPubKeyIndex(temp.pub_key); content = XCRYPT.Decrypt(content, pub_keyindex, XCRYPT.ZoneServerPrivKey); temp.body = content; packets.Add(temp); } return(packets.ToArray()); } catch (Exception e) { Console.WriteLine(e.ToString()); } } } return(packets.ToArray()); }
public ReadPacket(byte[] data, byte key) { try { buffer = data; using (MemoryStream ms = new MemoryStream(buffer)) { using (BinaryReader br = new BinaryReader(ms, Encoding.UTF8)) { short PacketLen = br.ReadInt16(); byte pubkey = br.ReadByte(); Head.len = PacketLen; Head.pubkey = pubkey; byte[] content = br.ReadBytes(PacketLen); using (MemoryStream ms2 = new MemoryStream()) { using (BinaryWriter bw = new BinaryWriter(ms2)) { byte PubKeyIndex = XCRYPT.GetPubKeyIndex(pubkey); bw.Write(XCRYPT.Decrypt(content, PubKeyIndex, key)); } ms2.Flush(); body = ms2.GetBuffer(); Array.Resize(ref body, content.Length); } } } Initialized = true; } catch { Initialized = false; return; } }