Skip to content

Commit 305a874

Browse files
committed
Restore CbcCipherMode
1 parent 47e324d commit 305a874

File tree

1 file changed

+100
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)