BlockCipherTest.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Linq;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. using Renci.SshNet.Security.Cryptography;
  5. using Renci.SshNet.Security.Cryptography.Ciphers;
  6. using Renci.SshNet.Security.Cryptography.Ciphers.Paddings;
  7. using Renci.SshNet.Tests.Common;
  8. namespace Renci.SshNet.Tests.Classes.Security.Cryptography
  9. {
  10. [TestClass]
  11. public class BlockCipherTest : TestBase
  12. {
  13. [TestMethod]
  14. public void EncryptShouldTakeIntoAccountPaddingForLengthOfOutputBufferPassedToEncryptBlock()
  15. {
  16. var input = new byte[] { 0x2c, 0x1a, 0x05, 0x00, 0x68 };
  17. var output = new byte[] { 0x0a, 0x00, 0x03, 0x02, 0x06, 0x08, 0x07, 0x05 };
  18. var key = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
  19. var blockCipher = new BlockCipherStub(key, 8, null, new PKCS7Padding())
  20. {
  21. EncryptBlockDelegate = (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset) =>
  22. {
  23. Assert.AreEqual(8, outputBuffer.Length);
  24. Buffer.BlockCopy(output, 0, outputBuffer, 0, output.Length);
  25. return inputBuffer.Length;
  26. }
  27. };
  28. var actual = blockCipher.Encrypt(input);
  29. Assert.IsTrue(output.SequenceEqual(actual));
  30. }
  31. [TestMethod]
  32. public void DecryptShouldTakeIntoAccountPaddingForLengthOfOutputBufferPassedToDecryptBlock()
  33. {
  34. var input = new byte[] { 0x2c, 0x1a, 0x05, 0x00, 0x68 };
  35. var output = new byte[] { 0x0a, 0x00, 0x03, 0x02, 0x06, 0x08, 0x07, 0x05 };
  36. var key = new byte[] { 0x17, 0x78, 0x56, 0xe1, 0x3e, 0xbd, 0x3e, 0x50, 0x1d, 0x79, 0x3f, 0x0f, 0x55, 0x37, 0x45, 0x54 };
  37. var blockCipher = new BlockCipherStub(key, 8, null, new PKCS7Padding())
  38. {
  39. DecryptBlockDelegate = (inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset) =>
  40. {
  41. Assert.AreEqual(8, outputBuffer.Length);
  42. Buffer.BlockCopy(output, 0, outputBuffer, 0, output.Length);
  43. return inputBuffer.Length;
  44. }
  45. };
  46. var actual = blockCipher.Decrypt(input);
  47. Assert.IsTrue(output.SequenceEqual(actual));
  48. }
  49. private class BlockCipherStub : BlockCipher
  50. {
  51. public Func<byte[], int, int, byte[], int, int> EncryptBlockDelegate;
  52. public Func<byte[], int, int, byte[], int, int> DecryptBlockDelegate;
  53. public BlockCipherStub(byte[] key, byte blockSize, CipherMode mode, CipherPadding padding) : base(key, blockSize, mode, padding)
  54. {
  55. }
  56. public override int EncryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  57. {
  58. return EncryptBlockDelegate(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
  59. }
  60. public override int DecryptBlock(byte[] inputBuffer, int inputOffset, int inputCount, byte[] outputBuffer, int outputOffset)
  61. {
  62. return DecryptBlockDelegate(inputBuffer, inputOffset, inputCount, outputBuffer, outputOffset);
  63. }
  64. }
  65. }
  66. }