TestBlowfish.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Microsoft.VisualStudio.TestTools.UnitTesting;
  6. namespace Renci.SshClient.Tests.Security.Cryptography
  7. {
  8. [TestClass]
  9. public class TestBlowfish
  10. {
  11. [TestMethod]
  12. public void Test_Blowfish_CBC_NoPadding()
  13. {
  14. var key = new byte[] { 0xe0, 0xb1, 0xb3, 0xbc, 0xa7, 0xe2, 0x2b, 0xca, 0xf0, 0xc1, 0xc7, 0xaa, 0x93, 0x50, 0xb7, 0x72 };
  15. var iv = new byte[] { 0x0b, 0x7c, 0xf1, 0x33, 0xe0, 0x4a, 0x64, 0x30, 0x37, 0x08, 0x83, 0xd3, 0x56, 0x04, 0x12, 0xd9, 0x88, 0xd0, 0x89, 0xf2, 0xe9, 0xd9, 0x25, 0xea, 0x81, 0x64, 0x5b, 0x03, 0xf2, 0xd3, 0xd5, 0x11 };
  16. var input = new byte[] { 0x00, 0x00, 0x00, 0x1c, 0x0a, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x73, 0x68, 0x2d, 0x75, 0x73, 0x65, 0x72, 0x61, 0x75, 0x74, 0x68, 0x4c, 0x78, 0x42, 0xbb, 0x1a, 0x87, 0x5e, 0xba, 0xfc, 0x9e };
  17. var correctEncoding = new byte[] { 0x42, 0x36, 0xf4, 0xe9, 0xfd, 0x47, 0x97, 0x43, 0x11, 0x0a, 0x52, 0xf6, 0x1e, 0x7b, 0x78, 0xc0, 0x34, 0x73, 0x58, 0xce, 0x0c, 0xd9, 0x20, 0x97, 0x76, 0x03, 0xb0, 0x2d, 0x4b, 0xce, 0x33, 0x8f };
  18. var encodedResult = new byte[input.Length];
  19. var decodedResult = new byte[input.Length];
  20. var cipher = new Renci.SshClient.Security.Cryptography.Blowfish();
  21. cipher.Mode = System.Security.Cryptography.CipherMode.CBC;
  22. var enc = cipher.CreateEncryptor(key, iv);
  23. enc.TransformBlock(input, 0, input.Length, encodedResult, 0);
  24. var dec = cipher.CreateDecryptor(key, iv);
  25. dec.TransformBlock(encodedResult, 0, encodedResult.Length, decodedResult, 0);
  26. Assert.IsTrue(encodedResult.IsEqualTo(correctEncoding));
  27. Assert.IsTrue(decodedResult.IsEqualTo(input));
  28. }
  29. [TestMethod]
  30. public void Test_Blowfish_128_CBC_NoPadding()
  31. {
  32. var key = new byte[] { 0xe0, 0xb1, 0xb3, 0xbc, 0xa7, 0xe2, 0x2b, 0xca, 0xf0, 0xc1, 0xc7, 0xaa, 0x93, 0x50, 0xb7, 0x72 };
  33. var iv = new byte[] { 0x0b, 0x7c, 0xf1, 0x33, 0xe0, 0x4a, 0x64, 0x30, 0x37, 0x08, 0x83, 0xd3, 0x56, 0x04, 0x12, 0xd9, 0x88, 0xd0, 0x89, 0xf2, 0xe9, 0xd9, 0x25, 0xea, 0x81, 0x64, 0x5b, 0x03, 0xf2, 0xd3, 0xd5, 0x11 };
  34. var input = new byte[] { 0x00, 0x00, 0x00, 0x1c, 0x0a, 0x05, 0x00, 0x00, 0x00, 0x0c, 0x73, 0x73, 0x68, 0x2d, 0x75, 0x73, 0x65, 0x72, 0x61, 0x75, 0x74, 0x68, 0x4c, 0x78, 0x42, 0xbb, 0x1a, 0x87, 0x5e, 0xba, 0xfc, 0x9e };
  35. var correctEncoding = new byte[] { 0x42, 0x36, 0xf4, 0xe9, 0xfd, 0x47, 0x97, 0x43, 0x11, 0x0a, 0x52, 0xf6, 0x1e, 0x7b, 0x78, 0xc0, 0x34, 0x73, 0x58, 0xce, 0x0c, 0xd9, 0x20, 0x97, 0x76, 0x03, 0xb0, 0x2d, 0x4b, 0xce, 0x33, 0x8f };
  36. var encodedResult = new byte[input.Length];
  37. var decodedResult = new byte[input.Length];
  38. var cipher = new Renci.SshClient.Security.Cryptography.Blowfish(128);
  39. cipher.Mode = System.Security.Cryptography.CipherMode.CBC;
  40. var enc = cipher.CreateEncryptor(key, iv);
  41. enc.TransformBlock(input, 0, input.Length, encodedResult, 0);
  42. var dec = cipher.CreateDecryptor(key, iv);
  43. dec.TransformBlock(encodedResult, 0, encodedResult.Length, decodedResult, 0);
  44. Assert.IsTrue(encodedResult.IsEqualTo(correctEncoding));
  45. Assert.IsTrue(decodedResult.IsEqualTo(input));
  46. }
  47. }
  48. }