| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 | using System;using System.Collections.Generic;using System.Linq;using System.Text;using Renci.SshNet.Security.Cryptography.Ciphers;namespace Renci.SshNet.Security.Cryptography{    /// <summary>    /// Base class for block cipher implementations.    /// </summary>    public abstract class BlockCipher : SymmetricCipher    {        private CipherMode _mode;        private CipherPadding _padding;        /// <summary>        /// Gets the size of the block in bytes.        /// </summary>        /// <value>        /// The size of the block in bytes.        /// </value>        protected readonly int _blockSize;        /// <summary>        /// Gets the size of the block.        /// </summary>        /// <value>        /// The size of the block.        /// </value>        public int BlockSize        {            get            {                return this._blockSize;            }        }        /// <summary>        /// Initializes a new instance of the <see cref="BlockCipher"/> class.        /// </summary>        /// <param name="key">The key.</param>        /// <param name="blockSize">Size of the block.</param>        /// <param name="mode">Cipher mode.</param>        /// <param name="padding">Cipher padding.</param>        /// <exception cref="ArgumentNullException"><paramref name="key"/> is null.</exception>        protected BlockCipher(byte[] key, int blockSize, CipherMode mode, CipherPadding padding)            : base(key)        {            this._blockSize = blockSize;            this._mode = mode;            this._padding = padding;            if (this._mode != null)                this._mode.Init(this);        }        /// <summary>        /// Encrypts the specified data.        /// </summary>        /// <param name="data">The data.</param>        /// <returns>Encrypted data</returns>        public override byte[] Encrypt(byte[] data)        {            var output = new byte[data.Length];            if (data.Length % this._blockSize > 0)            {                if (this._padding == null)                {                    throw new ArgumentException("data");                }                else                {                    data = this._padding.Pad(this._blockSize, data);                }            }            var writtenBytes = 0;            for (int i = 0; i < data.Length / this._blockSize; i++)            {                if (this._mode == null)                {                    writtenBytes += this.EncryptBlock(data, i * this._blockSize, this._blockSize, output, i * this._blockSize);                }                else                {                    writtenBytes += this._mode.EncryptBlock(data, i * this._blockSize, this._blockSize, output, i * this._blockSize);                }            }            if (writtenBytes < data.Length)            {                throw new InvalidOperationException("Encryption error.");            }            return output;        }        /// <summary>        /// Decrypts the specified data.        /// </summary>        /// <param name="data">The data.</param>        /// <returns>Decrypted data</returns>        public override byte[] Decrypt(byte[] data)        {            if (data.Length % this._blockSize > 0)            {                {                    if (this._padding == null)                    {                        throw new ArgumentException("data");                    }                    else                    {                        data = this._padding.Pad(this._blockSize, data);                    }                }            }            var output = new byte[data.Length];            var writtenBytes = 0;            for (int i = 0; i < data.Length / this._blockSize; i++)            {                if (this._mode == null)                {                    writtenBytes += this.DecryptBlock(data, i * this._blockSize, this._blockSize, output, i * this._blockSize);                }                else                {                    writtenBytes += this._mode.DecryptBlock(data, i * this._blockSize, this._blockSize, output, i * this._blockSize);                }            }            if (writtenBytes < data.Length)            {                throw new InvalidOperationException("Encryption error.");            }            return output;        }    }}
 |