/// <summary> /// Decrypts the given packet in-place. /// </summary> /// <remarks> /// The array will be modified directly. /// </remarks> /// <param name="packet">The data to decrypt.</param> public void Decrypt(byte[] packet) { lock (_decryptor) { _decryptor.Transform(packet); CustomCrypto.Decrypt(packet); } }
/// <summary> /// Encrypts a packet, constructs a header for it and packs them into a new array. /// </summary> /// <remarks> /// The array given as the <paramref name="packetData"/> parameter is transformed in-place. /// </remarks> /// <param name="packetData">The packet data to encrypt and pack.</param> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="packetData"/> is <see langword="null"/>. /// </exception> /// <returns>an array with the encrypted packet and its header.</returns> public byte[] EncryptAndPack(byte[] packetData) { Guard.NotNull(() => packetData, packetData); int length = packetData.Length; var rawData = new byte[length + 4]; lock (_encryptor) { byte[] header = ConstructHeader(length); Buffer.BlockCopy(header, 0, rawData, 0, 4); CustomCrypto.Encrypt(packetData); _encryptor.Transform(packetData); } Buffer.BlockCopy(packetData, 0, rawData, 4, length); return(rawData); }