Encrypt() public static method

Encrypts an array in-place.
The array given in data will be modified.
Thrown if is .
public static Encrypt ( byte data ) : void
data byte The array to encrypt.
return void
Esempio n. 1
0
        /// <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);
        }