public void Encrypt(NetOutgoingMessage msg, NetConnection reciever = null) { ConnectionCryptoProviders.TryGetValue(reciever, out var provider); if (provider == null) { return; } int unEncLenBits = msg.LengthBits; var ms = new MemoryStream(); var cs = new CryptoStream(ms, provider.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(msg.m_data, 0, msg.LengthBytes); cs.Close(); // get results var arr = ms.ToArray(); ms.Close(); msg.EnsureBufferSize((arr.Length + 4) * 8); msg.LengthBits = 0; // reset write pointer msg.Write((uint)unEncLenBits); msg.Write(arr); msg.LengthBits = (arr.Length + 4) * 8; }
public void EncryptHail(NetOutgoingMessage msg, string token, NetConnection reciever = null) { var provider = _connectionCryptoProvider; int unEncLenBits = msg.LengthBits; var ms = new MemoryStream(); var cs = new CryptoStream(ms, provider.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(msg.m_data, 0, msg.LengthBytes); cs.Close(); // get results var arr = ms.ToArray(); ms.Close(); msg.EnsureBufferSize((arr.Length + 4 + token.Length) * 8); msg.LengthBits = 0; // reset write pointer msg.Write(token); var tokenLength = msg.LengthBytes; msg.Write((uint)unEncLenBits); msg.Write(arr); msg.LengthBits = (arr.Length + 4 + tokenLength) * 8; }
public static NetOutgoingMessage Compress(NetOutgoingMessage msg) { msg.WritePadBits(); byte[] input = msg.PeekDataBuffer(); byte[] output = new byte[input.Length * 2]; int outlength = ZipCompressor.Instance.Compress(input, msg.LengthBytes, output); msg.EnsureBufferSize(outlength * 8); input = msg.PeekDataBuffer(); Array.Copy(output, input, outlength); msg.LengthBytes = outlength; msg.LengthBits = outlength * 8; return(msg); }
/// <summary>Write a memory stream in a format readable by ReadByteArray. Resets the memory stream position to zero.</summary> public static void WriteMemoryStreamAsByteArray(this NetOutgoingMessage message, MemoryStream ms) { Debug.Assert(ms == null || ms.Length <= 1024 * 1024 * 1024); // 1GB is stupidly large message.WritePadBits(); if (ms == null || ms.Length == 0) { message.WriteVariableUInt32(0); } else { message.WriteVariableUInt32((uint)ms.Length); // Directly write into message buffer: ms.Position = 0; message.EnsureBufferSize((message.LengthBytes + (int)ms.Length) * 8); ms.Read(message.Data, message.LengthBytes, (int)ms.Length); message.LengthBytes += (int)ms.Length; } }
private void Write <T>(NetOutgoingMessage msg, T multiplayerEvent) where T : struct, IMyEvent { m_stream.Reset(msg.Data); m_stream.Position = msg.LengthBytes; m_writer.Write((byte)multiplayerEvent.EventType); try { multiplayerEvent.Write(new MyMessageWriter(m_writer)); msg.LengthBytes = (int)m_writer.BaseStream.Position; } catch (EndOfStreamException) { Debug.Fail("Message size is too small for this type of message!"); MyMwcLog.WriteLine("ERROR: message size is too small for this type of message!"); // This could happen sometimes, for example for extremely large inventories... msg.EnsureBufferSize(msg.LengthBits * 2); Write(msg, multiplayerEvent); } }
/// <summary> /// Encrypt am outgoing message with this algorithm; no writing can be done to the message after encryption, or message will be corrupted /// </summary> public bool Encrypt(NetOutgoingMessage msg) { int payloadBitLength = msg.LengthBits; int numBytes = msg.LengthBytes; int blockSize = BlockSize; int numBlocks = (int)Math.Ceiling((double)numBytes / (double)blockSize); int dstSize = numBlocks * blockSize; msg.EnsureBufferSize(dstSize * 8 + (4 * 8)); // add 4 bytes for payload length at end msg.LengthBits = dstSize * 8; // length will automatically adjust +4 bytes when payload length is written for (int i = 0; i < numBlocks; i++) { EncryptBlock(msg.m_data, (i * blockSize), m_tmp); Buffer.BlockCopy(m_tmp, 0, msg.m_data, (i * blockSize), m_tmp.Length); } // add true payload length last msg.Write((UInt32)payloadBitLength); return true; }