DesCipherTest.cs 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System.Text;
  2. using Microsoft.VisualStudio.TestTools.UnitTesting;
  3. using Renci.SshNet.Common;
  4. using Renci.SshNet.Security.Cryptography.Ciphers;
  5. using Renci.SshNet.Security.Cryptography.Ciphers.Modes;
  6. using Renci.SshNet.Tests.Common;
  7. namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers
  8. {
  9. /// <summary>
  10. /// Implements DES cipher algorithm.
  11. /// </summary>
  12. [TestClass]
  13. public class DesCipherTest : TestBase
  14. {
  15. [TestMethod]
  16. public void Cbc_Encrypt()
  17. {
  18. var expectedCypher = new byte[]
  19. {
  20. 0x15, 0x43, 0x3e, 0x97, 0x65, 0x66, 0xea, 0x81, 0x22, 0xab, 0xe3,
  21. 0x11, 0x0f, 0x7d, 0xcb, 0x78, 0x56, 0x91, 0x22, 0x3d, 0xd6, 0xca,
  22. 0xe3, 0xbd
  23. };
  24. var input = Encoding.ASCII.GetBytes("www.javaCODEgeeks.com!!!");
  25. var key = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
  26. var iv = new byte[] { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
  27. var des = new DesCipher(key, new CbcCipherMode(iv), padding: null);
  28. var actualCypher = des.Encrypt(input);
  29. Assert.IsTrue((expectedCypher.IsEqualTo(actualCypher)));
  30. }
  31. [TestMethod]
  32. public void Cbc_Decrypt()
  33. {
  34. var expectedPlain = Encoding.ASCII.GetBytes("www.javaCODEgeeks.com!!!");
  35. var key = new byte[] { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef };
  36. var iv = new byte[] { 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00 };
  37. var cypher = new byte[]
  38. {
  39. 0x15, 0x43, 0x3e, 0x97, 0x65, 0x66, 0xea, 0x81, 0x22, 0xab, 0xe3,
  40. 0x11, 0x0f, 0x7d, 0xcb, 0x78, 0x56, 0x91, 0x22, 0x3d, 0xd6, 0xca,
  41. 0xe3, 0xbd
  42. };
  43. var des = new DesCipher(key, new CbcCipherMode(iv), padding: null);
  44. var plain = des.Decrypt(cypher);
  45. Assert.IsTrue(expectedPlain.IsEqualTo(plain));
  46. }
  47. }
  48. }