| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | |
| | | 4 | | namespace Renci.SshNet.Security.Cryptography.Ciphers.Modes |
| | | 5 | | { |
| | | 6 | | /// <summary> |
| | | 7 | | /// Implements OFB cipher mode. |
| | | 8 | | /// </summary> |
| | | 9 | | public class OfbCipherMode : CipherMode |
| | | 10 | | { |
| | | 11 | | private readonly byte[] _ivOutput; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Initializes a new instance of the <see cref="OfbCipherMode"/> class. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <param name="iv">The iv.</param> |
| | | 17 | | public OfbCipherMode(byte[] iv) |
| | 54 | 18 | | : base(iv) |
| | 54 | 19 | | { |
| | 54 | 20 | | _ivOutput = new byte[iv.Length]; |
| | 54 | 21 | | } |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Encrypts the specified region of the input byte array and copies the encrypted data to the specified region |
| | | 25 | | /// </summary> |
| | | 26 | | /// <param name="inputBuffer">The input data to encrypt.</param> |
| | | 27 | | /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param> |
| | | 28 | | /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param> |
| | | 29 | | /// <param name="outputBuffer">The output to which to write encrypted data.</param> |
| | | 30 | | /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param> |
| | | 31 | | /// <returns> |
| | | 32 | | /// The number of bytes encrypted. |
| | | 33 | | /// </returns> |
| | | 34 | | public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int o |
| | 126 | 35 | | { |
| | 126 | 36 | | if (inputBuffer.Length - inputOffset < _blockSize) |
| | 0 | 37 | | { |
| | 0 | 38 | | throw new ArgumentException("Invalid input buffer"); |
| | | 39 | | } |
| | | 40 | | |
| | 126 | 41 | | if (outputBuffer.Length - outputOffset < _blockSize) |
| | 0 | 42 | | { |
| | 0 | 43 | | throw new ArgumentException("Invalid output buffer"); |
| | | 44 | | } |
| | | 45 | | |
| | 126 | 46 | | if (inputCount != _blockSize) |
| | 0 | 47 | | { |
| | 0 | 48 | | throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "inputCount must be {0}.", _blockS |
| | | 49 | | } |
| | | 50 | | |
| | 126 | 51 | | _ = Cipher.EncryptBlock(IV, 0, IV.Length, _ivOutput, 0); |
| | | 52 | | |
| | 126 | 53 | | Buffer.BlockCopy(_ivOutput, 0, IV, 0, IV.Length); |
| | | 54 | | |
| | 4284 | 55 | | for (var i = 0; i < _blockSize; i++) |
| | 2016 | 56 | | { |
| | 2016 | 57 | | outputBuffer[outputOffset + i] = (byte) (_ivOutput[i] ^ inputBuffer[inputOffset + i]); |
| | 2016 | 58 | | } |
| | | 59 | | |
| | 126 | 60 | | return _blockSize; |
| | 126 | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Decrypts the specified region of the input byte array and copies the decrypted data to the specified region |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="inputBuffer">The input data to decrypt.</param> |
| | | 67 | | /// <param name="inputOffset">The offset into the input byte array from which to begin using data.</param> |
| | | 68 | | /// <param name="inputCount">The number of bytes in the input byte array to use as data.</param> |
| | | 69 | | /// <param name="outputBuffer">The output to which to write decrypted data.</param> |
| | | 70 | | /// <param name="outputOffset">The offset into the output byte array from which to begin writing data.</param> |
| | | 71 | | /// <returns> |
| | | 72 | | /// The number of bytes decrypted. |
| | | 73 | | /// </returns> |
| | | 74 | | public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int o |
| | 63 | 75 | | { |
| | 63 | 76 | | return EncryptBlock(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset); |
| | 63 | 77 | | } |
| | | 78 | | } |
| | | 79 | | } |