Exemplo n.º 1
0
        /// <summary>
        /// Encrypts binary data
        /// </summary>
        /// <param name="Data">Data to encrypt.</param>
        /// <param name="Encrypted">Encrypted data will be stored here.</param>
        /// <param name="Key">Encryption Key</param>
        /// <param name="IV">Initiation Vector</param>
        /// <param name="AssociatedData">Any associated data used for authenticated encryption (AEAD).</param>
        public override async Task Encrypt(Stream Data, Stream Encrypted, byte[] Key, byte[] IV, byte[] AssociatedData)
        {
            Security.ChaChaPoly.AeadChaCha20Poly1305 Acp = new Security.ChaChaPoly.AeadChaCha20Poly1305(Key, IV);

            byte[] Mac = await Acp.Encrypt(Data, Encrypted, AssociatedData);

            await Encrypted.WriteAsync(Mac, 0, 16);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Encrypts binary data
        /// </summary>
        /// <param name="Data">Binary Data</param>
        /// <param name="Key">Encryption Key</param>
        /// <param name="IV">Initiation Vector</param>
        /// <param name="AssociatedData">Any associated data used for authenticated encryption (AEAD).</param>
        /// <returns>Encrypted Data</returns>
        public override byte[] Encrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData)
        {
            Security.ChaChaPoly.AeadChaCha20Poly1305 Acp = new Security.ChaChaPoly.AeadChaCha20Poly1305(Key, IV);

            byte[] Encrypted = Acp.Encrypt(Data, AssociatedData, out byte[] Mac);
            int    c         = Encrypted.Length;

            Array.Resize <byte>(ref Encrypted, c + 16);
            Array.Copy(Mac, 0, Encrypted, c, 16);

            return(Encrypted);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Decrypts binary data
        /// </summary>
        /// <param name="Data">Binary Data</param>
        /// <param name="Key">Encryption Key</param>
        /// <param name="IV">Initiation Vector</param>
        /// <param name="AssociatedData">Any associated data used for authenticated encryption (AEAD).</param>
        /// <returns>Decrypted Data</returns>
        public override byte[] Decrypt(byte[] Data, byte[] Key, byte[] IV, byte[] AssociatedData)
        {
            int c = Data.Length;

            if (c < 16)
            {
                return(null);
            }

            Security.ChaChaPoly.AeadChaCha20Poly1305 Acp = new Security.ChaChaPoly.AeadChaCha20Poly1305(Key, IV);
            byte[] Mac = new byte[16];
            Array.Copy(Data, c - 16, Mac, 0, 16);

            Array.Resize <byte>(ref Data, c - 16);

            return(Acp.Decrypt(Data, AssociatedData, Mac));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Decrypts binary data
        /// </summary>
        /// <param name="Data">Binary Data</param>
        /// <param name="Key">Encryption Key</param>
        /// <param name="IV">Initiation Vector</param>
        /// <param name="AssociatedData">Any associated data used for authenticated encryption (AEAD).</param>
        /// <returns>Decrypted Data</returns>
        public override async Task <Stream> Decrypt(Stream Data, byte[] Key, byte[] IV, byte[] AssociatedData)
        {
            long c = Data.Length;

            if (c < 16)
            {
                return(null);
            }

            Security.ChaChaPoly.AeadChaCha20Poly1305 Acp = new Security.ChaChaPoly.AeadChaCha20Poly1305(Key, IV);

            using (TemporaryStream Temp = new TemporaryStream())
            {
                Data.Position = 0;
                await Crypto.CopyAsync(Data, Temp, c - 16);

                byte[] Mac = new byte[16];

                await Data.ReadAsync(Mac, 0, 16);

                Temp.Position = 0;
                return(await Acp.Decrypt(Temp, AssociatedData, Mac));
            }
        }