DesCipherTest.cs 2.1 KB

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