Skip to content

Commit 6a1859c

Browse files
committed
Added Decrypt overload taking offset and length.
1 parent ad3de36 commit 6a1859c

File tree

9 files changed

+185
-79
lines changed

9 files changed

+185
-79
lines changed

src/Renci.SshNet.Tests/Classes/PrivateKeyFileTest.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public void ConstructorWithPrivateKeyAndPassphraseShouldThrowArgumentNullExcepti
140140
[TestCategory("PrivateKey")]
141141
public void Test_PrivateKey_RSA()
142142
{
143-
using (var stream = this.GetData("Key.RSA.txt"))
143+
using (var stream = GetData("Key.RSA.txt"))
144144
{
145145
new PrivateKeyFile(stream);
146146
}
@@ -151,7 +151,7 @@ public void Test_PrivateKey_RSA()
151151
[TestCategory("PrivateKey")]
152152
public void Test_PrivateKey_SSH2_DSA()
153153
{
154-
using (var stream = this.GetData("Key.SSH2.DSA.txt"))
154+
using (var stream = GetData("Key.SSH2.DSA.txt"))
155155
{
156156
new PrivateKeyFile(stream);
157157
}
@@ -162,7 +162,7 @@ public void Test_PrivateKey_SSH2_DSA()
162162
[TestCategory("PrivateKey")]
163163
public void Test_PrivateKey_SSH2_RSA()
164164
{
165-
using (var stream = this.GetData("Key.SSH2.RSA.txt"))
165+
using (var stream = GetData("Key.SSH2.RSA.txt"))
166166
{
167167
new PrivateKeyFile(stream);
168168
}
@@ -173,7 +173,7 @@ public void Test_PrivateKey_SSH2_RSA()
173173
[TestCategory("PrivateKey")]
174174
public void Test_PrivateKey_SSH2_Encrypted_DSA_DES_CBC()
175175
{
176-
using (var stream = this.GetData("Key.SSH2.DSA.Encrypted.Des.CBC.12345.txt"))
176+
using (var stream = GetData("Key.SSH2.DSA.Encrypted.Des.CBC.12345.txt"))
177177
{
178178
new PrivateKeyFile(stream, "12345");
179179
}
@@ -184,7 +184,7 @@ public void Test_PrivateKey_SSH2_Encrypted_DSA_DES_CBC()
184184
[TestCategory("PrivateKey")]
185185
public void Test_PrivateKey_SSH2_Encrypted_RSA_DES_CBC()
186186
{
187-
using (var stream = this.GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
187+
using (var stream = GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
188188
{
189189
new PrivateKeyFile(stream, "12345");
190190
}
@@ -195,7 +195,7 @@ public void Test_PrivateKey_SSH2_Encrypted_RSA_DES_CBC()
195195
[TestCategory("PrivateKey")]
196196
public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshExceptionWhenPassphraseIsWrong()
197197
{
198-
using (var stream = this.GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
198+
using (var stream = GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
199199
{
200200
try
201201
{
@@ -216,7 +216,7 @@ public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshExceptionWhenPassphrase
216216
[TestCategory("PrivateKey")]
217217
public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPassphraseIsNull()
218218
{
219-
using (var stream = this.GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
219+
using (var stream = GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
220220
{
221221
try
222222
{
@@ -237,7 +237,7 @@ public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyEx
237237
[TestCategory("PrivateKey")]
238238
public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyExceptionWhenPassphraseIsEmpty()
239239
{
240-
using (var stream = this.GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
240+
using (var stream = GetData("Key.SSH2.RSA.Encrypted.Des.CBC.12345.txt"))
241241
{
242242
try
243243
{
@@ -258,7 +258,7 @@ public void Test_PrivateKey_SSH2_Encrypted_ShouldThrowSshPassPhraseNullOrEmptyEx
258258
[TestCategory("PrivateKey")]
259259
public void Test_PrivateKey_RSA_DES_CBC()
260260
{
261-
using (var stream = this.GetData("Key.RSA.Encrypted.Des.CBC.12345.txt"))
261+
using (var stream = GetData("Key.RSA.Encrypted.Des.CBC.12345.txt"))
262262
{
263263
new PrivateKeyFile(stream, "12345");
264264
}
@@ -269,7 +269,7 @@ public void Test_PrivateKey_RSA_DES_CBC()
269269
[TestCategory("PrivateKey")]
270270
public void Test_PrivateKey_RSA_DES_EDE3_CBC()
271271
{
272-
using (var stream = this.GetData("Key.RSA.Encrypted.Des.Ede3.CBC.12345.txt"))
272+
using (var stream = GetData("Key.RSA.Encrypted.Des.Ede3.CBC.12345.txt"))
273273
{
274274
new PrivateKeyFile(stream, "12345");
275275
}
@@ -280,7 +280,7 @@ public void Test_PrivateKey_RSA_DES_EDE3_CBC()
280280
[TestCategory("PrivateKey")]
281281
public void Test_PrivateKey_RSA_AES_128_CBC()
282282
{
283-
using (var stream = this.GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
283+
using (var stream = GetData("Key.RSA.Encrypted.Aes.128.CBC.12345.txt"))
284284
{
285285
new PrivateKeyFile(stream, "12345");
286286
}
@@ -291,7 +291,7 @@ public void Test_PrivateKey_RSA_AES_128_CBC()
291291
[TestCategory("PrivateKey")]
292292
public void Test_PrivateKey_RSA_AES_192_CBC()
293293
{
294-
using (var stream = this.GetData("Key.RSA.Encrypted.Aes.192.CBC.12345.txt"))
294+
using (var stream = GetData("Key.RSA.Encrypted.Aes.192.CBC.12345.txt"))
295295
{
296296
new PrivateKeyFile(stream, "12345");
297297
}
@@ -302,7 +302,7 @@ public void Test_PrivateKey_RSA_AES_192_CBC()
302302
[TestCategory("PrivateKey")]
303303
public void Test_PrivateKey_RSA_AES_256_CBC()
304304
{
305-
using (var stream = this.GetData("Key.RSA.Encrypted.Aes.256.CBC.12345.txt"))
305+
using (var stream = GetData("Key.RSA.Encrypted.Aes.256.CBC.12345.txt"))
306306
{
307307
new PrivateKeyFile(stream, "12345");
308308
}
@@ -313,7 +313,7 @@ public void Test_PrivateKey_RSA_AES_256_CBC()
313313
[TestCategory("PrivateKey")]
314314
public void Test_PrivateKey_RSA_DES_EDE3_CFB()
315315
{
316-
using (var stream = this.GetData("Key.RSA.Encrypted.Des.Ede3.CFB.1234567890.txt"))
316+
using (var stream = GetData("Key.RSA.Encrypted.Des.Ede3.CFB.1234567890.txt"))
317317
{
318318
new PrivateKeyFile(stream, "1234567890");
319319
}

src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/AesCipherTest.cs

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
using System;
2-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
32
using Renci.SshNet.Security.Cryptography.Ciphers;
43
using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
54
using Renci.SshNet.Tests.Common;
65
using Renci.SshNet.Tests.Properties;
7-
using System.Linq;
8-
using Renci.SshNet.Common;
96

107
namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers
118
{
@@ -16,21 +13,36 @@ namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers
1613
public class AesCipherTest : TestBase
1714
{
1815
[TestMethod]
19-
public void Test_Cipher_AES_128_CBC()
16+
public void Encrypt_Input_128_CBC()
2017
{
2118
var input = new byte[] { 0x00, 0x00, 0x00, 0x2c, 0x1a, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x73, 0x68, 0x2d, 0x75, 0x73, 0x65, 0x72, 0x61, 0x75, 0x74, 0x68, 0x30, 0x9e, 0xe0, 0x9c, 0x12, 0xee, 0x3a, 0x30, 0x03, 0x52, 0x1c, 0x1a, 0xe7, 0x3e, 0x0b, 0x9a, 0xcf, 0x9a, 0x57, 0x42, 0x0b, 0x4f, 0x4a, 0x15, 0xa0, 0xf5 };
2219
var key = new byte[] { 0xe4, 0x94, 0xf9, 0xb1, 0x00, 0x4f, 0x16, 0x2a, 0x80, 0x11, 0xea, 0x73, 0x0d, 0xb9, 0xbf, 0x64 };
2320
var iv = new byte[] { 0x74, 0x8b, 0x4f, 0xe6, 0xc1, 0x29, 0xb3, 0x54, 0xec, 0x77, 0x92, 0xf3, 0x15, 0xa0, 0x41, 0xa8 };
24-
var output = new byte[] { 0x19, 0x7f, 0x80, 0xd8, 0xc9, 0x89, 0xc4, 0xa7, 0xc6, 0xc6, 0x3f, 0x9f, 0x1e, 0x00, 0x1f, 0x72, 0xa7, 0x5e, 0xde, 0x40, 0x88, 0xa2, 0x72, 0xf2, 0xed, 0x3f, 0x81, 0x45, 0xb6, 0xbd, 0x45, 0x87, 0x15, 0xa5, 0x10, 0x92, 0x4a, 0x37, 0x9e, 0xa9, 0x80, 0x1c, 0x14, 0x83, 0xa3, 0x39, 0x45, 0x28 };
21+
var expected = new byte[] { 0x19, 0x7f, 0x80, 0xd8, 0xc9, 0x89, 0xc4, 0xa7, 0xc6, 0xc6, 0x3f, 0x9f, 0x1e, 0x00, 0x1f, 0x72, 0xa7, 0x5e, 0xde, 0x40, 0x88, 0xa2, 0x72, 0xf2, 0xed, 0x3f, 0x81, 0x45, 0xb6, 0xbd, 0x45, 0x87, 0x15, 0xa5, 0x10, 0x92, 0x4a, 0x37, 0x9e, 0xa9, 0x80, 0x1c, 0x14, 0x83, 0xa3, 0x39, 0x45, 0x28 };
2522
var testCipher = new AesCipher(key, new CbcCipherMode(iv), null);
26-
var r = testCipher.Encrypt(input);
2723

28-
if (!r.SequenceEqual(output))
29-
Assert.Fail("Invalid encryption");
24+
var actual = testCipher.Encrypt(input);
25+
26+
Assert.IsTrue(actual.IsEqualTo(expected));
3027
}
3128

3229
[TestMethod]
33-
public void Test_Cipher_AES_128_CTR()
30+
public void Encrypt_InputAndOffsetAndLength_128_CBC()
31+
{
32+
// 2 dummy bytes before and 3 after the input from test case above
33+
var input = new byte[] { 0x05, 0x07, 0x00, 0x00, 0x00, 0x2c, 0x1a, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x73, 0x68, 0x2d, 0x75, 0x73, 0x65, 0x72, 0x61, 0x75, 0x74, 0x68, 0x30, 0x9e, 0xe0, 0x9c, 0x12, 0xee, 0x3a, 0x30, 0x03, 0x52, 0x1c, 0x1a, 0xe7, 0x3e, 0x0b, 0x9a, 0xcf, 0x9a, 0x57, 0x42, 0x0b, 0x4f, 0x4a, 0x15, 0xa0, 0xf5, 0x0f, 0x0d, 0x03 };
34+
var key = new byte[] { 0xe4, 0x94, 0xf9, 0xb1, 0x00, 0x4f, 0x16, 0x2a, 0x80, 0x11, 0xea, 0x73, 0x0d, 0xb9, 0xbf, 0x64 };
35+
var iv = new byte[] { 0x74, 0x8b, 0x4f, 0xe6, 0xc1, 0x29, 0xb3, 0x54, 0xec, 0x77, 0x92, 0xf3, 0x15, 0xa0, 0x41, 0xa8 };
36+
var expected = new byte[] { 0x19, 0x7f, 0x80, 0xd8, 0xc9, 0x89, 0xc4, 0xa7, 0xc6, 0xc6, 0x3f, 0x9f, 0x1e, 0x00, 0x1f, 0x72, 0xa7, 0x5e, 0xde, 0x40, 0x88, 0xa2, 0x72, 0xf2, 0xed, 0x3f, 0x81, 0x45, 0xb6, 0xbd, 0x45, 0x87, 0x15, 0xa5, 0x10, 0x92, 0x4a, 0x37, 0x9e, 0xa9, 0x80, 0x1c, 0x14, 0x83, 0xa3, 0x39, 0x45, 0x28 };
37+
var testCipher = new AesCipher(key, new CbcCipherMode(iv), null);
38+
39+
var actual = testCipher.Encrypt(input, 2, input.Length - 5);
40+
41+
Assert.IsTrue(actual.IsEqualTo(expected));
42+
}
43+
44+
[TestMethod]
45+
public void Encrypt_Input_128_CTR()
3446
{
3547
var input = new byte[] { 0x00, 0x00, 0x00, 0x2c, 0x1a, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x73, 0x68, 0x2d, 0x75, 0x73, 0x65, 0x72, 0x61, 0x75, 0x74, 0x68, 0xb0, 0x74, 0x21, 0x87, 0x16, 0xb9, 0x69, 0x48, 0x33, 0xce, 0xb3, 0xe7, 0xdc, 0x3f, 0x50, 0xdc, 0xcc, 0xd5, 0x27, 0xb7, 0xfe, 0x7a, 0x78, 0x22, 0xae, 0xc8 };
3648
var key = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
@@ -44,17 +56,31 @@ public void Test_Cipher_AES_128_CTR()
4456
}
4557

4658
[TestMethod]
47-
public void Decrypt_AES_128_CTR()
59+
public void Decrypt_Input_128_CTR()
60+
{
61+
var key = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
62+
var iv = new byte[] { 0xe6, 0x65, 0x36, 0x0d, 0xdd, 0xd7, 0x50, 0xc3, 0x48, 0xdb, 0x48, 0x07, 0xa1, 0x30, 0xd2, 0x38 };
63+
var input = new byte[] { 0xca, 0xfb, 0x1c, 0x49, 0xbf, 0x82, 0x2a, 0xbb, 0x1c, 0x52, 0xc7, 0x86, 0x22, 0x8a, 0xe5, 0xa4, 0xf3, 0xda, 0x4e, 0x1c, 0x3a, 0x87, 0x41, 0x1c, 0xd2, 0x6e, 0x76, 0xdc, 0xc2, 0xe9, 0xc2, 0x0e, 0xf5, 0xc7, 0xbd, 0x12, 0x85, 0xfa, 0x0e, 0xda, 0xee, 0x50, 0xd7, 0xfd, 0x81, 0x34, 0x25, 0x6d };
64+
var expected = new byte[] { 0x00, 0x00, 0x00, 0x2c, 0x1a, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x73, 0x68, 0x2d, 0x75, 0x73, 0x65, 0x72, 0x61, 0x75, 0x74, 0x68, 0xb0, 0x74, 0x21, 0x87, 0x16, 0xb9, 0x69, 0x48, 0x33, 0xce, 0xb3, 0xe7, 0xdc, 0x3f, 0x50, 0xdc, 0xcc, 0xd5, 0x27, 0xb7, 0xfe, 0x7a, 0x78, 0x22, 0xae, 0xc8 };
65+
var testCipher = new AesCipher(key, new CtrCipherMode(iv), null);
66+
67+
var actual = testCipher.Decrypt(input);
68+
69+
Assert.IsTrue(expected.IsEqualTo(actual));
70+
}
71+
72+
[TestMethod]
73+
public void Decrypt_InputAndOffsetAndLength_128_CTR()
4874
{
49-
var input = new byte[] { 0x00, 0x00, 0x00, 0x2c, 0x1a, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x73, 0x68, 0x2d, 0x75, 0x73, 0x65, 0x72, 0x61, 0x75, 0x74, 0x68, 0xb0, 0x74, 0x21, 0x87, 0x16, 0xb9, 0x69, 0x48, 0x33, 0xce, 0xb3, 0xe7, 0xdc, 0x3f, 0x50, 0xdc, 0xcc, 0xd5, 0x27, 0xb7, 0xfe, 0x7a, 0x78, 0x22, 0xae, 0xc8 };
5075
var key = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
5176
var iv = new byte[] { 0xe6, 0x65, 0x36, 0x0d, 0xdd, 0xd7, 0x50, 0xc3, 0x48, 0xdb, 0x48, 0x07, 0xa1, 0x30, 0xd2, 0x38 };
52-
var output = new byte[] { 0xca, 0xfb, 0x1c, 0x49, 0xbf, 0x82, 0x2a, 0xbb, 0x1c, 0x52, 0xc7, 0x86, 0x22, 0x8a, 0xe5, 0xa4, 0xf3, 0xda, 0x4e, 0x1c, 0x3a, 0x87, 0x41, 0x1c, 0xd2, 0x6e, 0x76, 0xdc, 0xc2, 0xe9, 0xc2, 0x0e, 0xf5, 0xc7, 0xbd, 0x12, 0x85, 0xfa, 0x0e, 0xda, 0xee, 0x50, 0xd7, 0xfd, 0x81, 0x34, 0x25, 0x6d };
77+
var input = new byte[] { 0x0a, 0xca, 0xfb, 0x1c, 0x49, 0xbf, 0x82, 0x2a, 0xbb, 0x1c, 0x52, 0xc7, 0x86, 0x22, 0x8a, 0xe5, 0xa4, 0xf3, 0xda, 0x4e, 0x1c, 0x3a, 0x87, 0x41, 0x1c, 0xd2, 0x6e, 0x76, 0xdc, 0xc2, 0xe9, 0xc2, 0x0e, 0xf5, 0xc7, 0xbd, 0x12, 0x85, 0xfa, 0x0e, 0xda, 0xee, 0x50, 0xd7, 0xfd, 0x81, 0x34, 0x25, 0x6d, 0x0a, 0x05 };
78+
var expected = new byte[] { 0x00, 0x00, 0x00, 0x2c, 0x1a, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x73, 0x68, 0x2d, 0x75, 0x73, 0x65, 0x72, 0x61, 0x75, 0x74, 0x68, 0xb0, 0x74, 0x21, 0x87, 0x16, 0xb9, 0x69, 0x48, 0x33, 0xce, 0xb3, 0xe7, 0xdc, 0x3f, 0x50, 0xdc, 0xcc, 0xd5, 0x27, 0xb7, 0xfe, 0x7a, 0x78, 0x22, 0xae, 0xc8 };
5379
var testCipher = new AesCipher(key, new CtrCipherMode(iv), null);
5480

55-
var actual = testCipher.Decrypt(output);
81+
var actual = testCipher.Decrypt(input, 1, input.Length - 3);
5682

57-
Assert.IsTrue(input.IsEqualTo(actual));
83+
Assert.IsTrue(expected.IsEqualTo(actual));
5884
}
5985

6086
[TestMethod]

src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/Arc4CipherTest.cs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Text;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
3-
using Renci.SshNet.Common;
43
using Renci.SshNet.Security.Cryptography.Ciphers;
54
using Renci.SshNet.Tests.Common;
65
using Renci.SshNet.Tests.Properties;
@@ -53,6 +52,20 @@ public void Decrypt_DischargeFirstBytes_False2()
5352
Assert.AreEqual(expectedPlainText, encoding.GetString(actualPlainText));
5453
}
5554

55+
[TestMethod]
56+
public void Decrypt_InputAndOffsetAndLength()
57+
{
58+
const string key = "Key";
59+
const string expectedPlainText = "Plaintext";
60+
var encoding = Encoding.ASCII;
61+
var cipher = new Arc4Cipher(encoding.GetBytes(key), false);
62+
var cipherText = new byte[] { 0x0A, 0x0f, 0xBB, 0xF3, 0x16, 0xE8, 0xD9, 0x40, 0xAF, 0x0A, 0xD3, 0x0d, 0x05 };
63+
64+
var actualPlainText = cipher.Decrypt(cipherText, 2, cipherText.Length - 4);
65+
66+
Assert.AreEqual(expectedPlainText, encoding.GetString(actualPlainText));
67+
}
68+
5669
/// <summary>
5770
///A test for DecryptBlock
5871
///</summary>
@@ -86,16 +99,7 @@ public void Encrypt_DischargeFirstBytes_False1()
8699
var actualCipherText = cipher.Encrypt(encoding.GetBytes(plainText));
87100

88101
Assert.IsNotNull(actualCipherText);
89-
Assert.AreEqual(expectedCipherText.Length, actualCipherText.Length);
90-
Assert.AreEqual(expectedCipherText[0], actualCipherText[0]);
91-
Assert.AreEqual(expectedCipherText[1], actualCipherText[1]);
92-
Assert.AreEqual(expectedCipherText[2], actualCipherText[2]);
93-
Assert.AreEqual(expectedCipherText[3], actualCipherText[3]);
94-
Assert.AreEqual(expectedCipherText[4], actualCipherText[4]);
95-
Assert.AreEqual(expectedCipherText[5], actualCipherText[5]);
96-
Assert.AreEqual(expectedCipherText[6], actualCipherText[6]);
97-
Assert.AreEqual(expectedCipherText[7], actualCipherText[7]);
98-
Assert.AreEqual(expectedCipherText[8], actualCipherText[8]);
102+
Assert.IsTrue(expectedCipherText.IsEqualTo(actualCipherText));
99103
}
100104

101105
[TestMethod]
@@ -110,12 +114,25 @@ public void Encrypt_DischargeFirstBytes_False2()
110114
var actualCipherText = cipher.Encrypt(encoding.GetBytes(plainText));
111115

112116
Assert.IsNotNull(actualCipherText);
113-
Assert.AreEqual(expectedCipherText.Length, actualCipherText.Length);
114-
Assert.AreEqual(expectedCipherText[0], actualCipherText[0]);
115-
Assert.AreEqual(expectedCipherText[1], actualCipherText[1]);
116-
Assert.AreEqual(expectedCipherText[2], actualCipherText[2]);
117-
Assert.AreEqual(expectedCipherText[3], actualCipherText[3]);
118-
Assert.AreEqual(expectedCipherText[4], actualCipherText[4]);
117+
Assert.IsTrue(expectedCipherText.IsEqualTo(actualCipherText));
118+
}
119+
120+
[TestMethod]
121+
public void Encrypt_InputAndOffsetAndLength()
122+
{
123+
const string key = "Wiki";
124+
const string plainText = "NOpediaNO";
125+
var encoding = Encoding.ASCII;
126+
var cipher = new Arc4Cipher(encoding.GetBytes(key), false);
127+
var plainTextBytes = encoding.GetBytes(plainText);
128+
var expectedCipherText = new byte[] { 0x10, 0X21, 0xBF, 0x04, 0x20 };
129+
130+
var actualCipherText = cipher.Encrypt(plainTextBytes, 2, plainTextBytes.Length - 4);
131+
132+
Assert.IsNotNull(actualCipherText);
133+
Assert.IsTrue(expectedCipherText.IsEqualTo(actualCipherText));
134+
135+
Assert.IsTrue(plainTextBytes.IsEqualTo(encoding.GetBytes(plainText)));
119136
}
120137

121138
/// <summary>

src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/CastCipherTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void Test_Cipher_CastCipher_128_CBC()
2020
var key = new byte[] { 0xe4, 0x94, 0xf9, 0xb1, 0x00, 0x4f, 0x16, 0x2a, 0x80, 0x11, 0xea, 0x73, 0x0d, 0xb9, 0xbf, 0x64 };
2121
var iv = new byte[] { 0x74, 0x8b, 0x4f, 0xe6, 0xc1, 0x29, 0xb3, 0x54, 0xec, 0x77, 0x92, 0xf3, 0x15, 0xa0, 0x41, 0xa8 };
2222
var output = new byte[] { 0x32, 0xef, 0xbd, 0xac, 0xb6, 0xfd, 0x1f, 0xae, 0x1b, 0x13, 0x5f, 0x31, 0x6d, 0x38, 0xcd, 0xb0, 0xe3, 0xca, 0xe1, 0xbc, 0xf8, 0xa7, 0xc2, 0x31, 0x62, 0x14, 0x3a, 0x9a, 0xda, 0xe3, 0xf8, 0xc8, 0x70, 0x87, 0x53, 0x21, 0x5d, 0xb7, 0x94, 0xb7, 0xe8, 0xc6, 0x9d, 0x46, 0x0c, 0x6d, 0x64, 0x6d };
23-
var testCipher = new Renci.SshNet.Security.Cryptography.Ciphers.CastCipher(key, new Renci.SshNet.Security.Cryptography.Ciphers.Modes.CbcCipherMode(iv), null);
23+
var testCipher = new CastCipher(key, new CbcCipherMode(iv), null);
2424
var r = testCipher.Encrypt(input);
2525

2626
if (!r.SequenceEqual(output))

src/Renci.SshNet.Tests/Classes/Security/Cryptography/Ciphers/TripleDesCipherTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void Test_Cipher_3DES_CBC()
2020
var key = new byte[] { 0x78, 0xf6, 0xc6, 0xbb, 0x57, 0x03, 0x69, 0xca, 0xba, 0x31, 0x18, 0x2f, 0x2f, 0x4c, 0x35, 0x34, 0x64, 0x06, 0x85, 0x30, 0xbe, 0x78, 0x60, 0xb3 };
2121
var iv = new byte[] { 0xc0, 0x75, 0xf2, 0x26, 0x0a, 0x2a, 0x42, 0x96 };
2222
var output = new byte[] { 0x28, 0x77, 0x2f, 0x07, 0x3e, 0xc2, 0x27, 0xa6, 0xdb, 0x36, 0x4d, 0xc6, 0x7a, 0x26, 0x7a, 0x38, 0xe6, 0x54, 0x0b, 0xab, 0x07, 0x87, 0xf0, 0xa4, 0x73, 0x1f, 0xde, 0xe6, 0x81, 0x1d, 0x4b, 0x4b };
23-
var testCipher = new Renci.SshNet.Security.Cryptography.Ciphers.TripleDesCipher(key, new Renci.SshNet.Security.Cryptography.Ciphers.Modes.CbcCipherMode(iv), null);
23+
var testCipher = new TripleDesCipher(key, new CbcCipherMode(iv), null);
2424
var r = testCipher.Encrypt(input);
2525

2626
if (!r.SequenceEqual(output))

0 commit comments

Comments
 (0)