public override bool Encrypt(NetOutgoingMessage msg) { int unEncLenBits = msg.LengthBits; var ms = new MemoryStream(); var cs = new CryptoStream(ms, m_algorithm.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(msg.m_data, 0, msg.LengthBytes); //cs.Close(); cs.Dispose(); // get results var arr = ms.ToArray(); //ms.Close(); ms.Dispose(); 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; return(true); }
/// <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 override bool Encrypt(NetOutgoingMessage msg) { int payloadBitLength = msg.LengthBits; int numBytes = msg.LengthBytes; int blockSize = BlockSize; Span <byte> tmp = stackalloc byte[BlockSize]; int numBlocks = (int)Math.Ceiling(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 var dataSpan = msg.m_data; for (int i = 0; i < numBlocks; i++) { // TODO: honestly is tmp even necessary here? EncryptBlock(dataSpan, (i * blockSize), tmp); //Buffer.BlockCopy(m_tmp, 0, dataSpan, (i * blockSize), m_tmp.Length); tmp.CopyTo(dataSpan.Slice(i * blockSize, tmp.Length)); } // add true payload length last msg.Write((UInt32)payloadBitLength); return(true); }
public override bool Encrypt(NetOutgoingMessage msg) { int unEncLenBits = msg.LengthBits; var ms = new MemoryStream(); var cs = GetEncryptStream(ms); 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; return true; }
/// <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; }
/// <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 override 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); }
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; }