Arc4CipherTest.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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.Tests.Common;
  6. namespace Renci.SshNet.Tests.Classes.Security.Cryptography.Ciphers
  7. {
  8. /// <summary>
  9. /// Implements ARCH4 cipher algorithm
  10. /// </summary>
  11. [TestClass]
  12. public class Arc4CipherTest : TestBase
  13. {
  14. [TestMethod]
  15. public void Decrypt_DischargeFirstBytes_False1()
  16. {
  17. const string key = "Key";
  18. const string expectedPlainText = "Plaintext";
  19. var encoding = Encoding.ASCII;
  20. var cipher = new Arc4Cipher(encoding.GetBytes(key), false);
  21. var cipherText = new byte[] { 0xBB, 0xF3, 0x16, 0xE8, 0xD9, 0x40, 0xAF, 0x0A, 0xD3 };
  22. var actualPlainText = cipher.Decrypt(cipherText);
  23. Assert.AreEqual(expectedPlainText, encoding.GetString(actualPlainText));
  24. }
  25. [TestMethod]
  26. public void Decrypt_DischargeFirstBytes_False2()
  27. {
  28. const string key = "Wiki";
  29. const string expectedPlainText = "pedia";
  30. var encoding = Encoding.ASCII;
  31. var cipher = new Arc4Cipher(encoding.GetBytes(key), false);
  32. var cipherText = new byte[] { 0x10, 0X21, 0xBF, 0x04, 0x20 };
  33. var actualPlainText = cipher.Decrypt(cipherText);
  34. Assert.AreEqual(expectedPlainText, encoding.GetString(actualPlainText));
  35. }
  36. [TestMethod]
  37. public void Decrypt_InputAndOffsetAndLength()
  38. {
  39. const string key = "Key";
  40. const string expectedPlainText = "Plaintext";
  41. var encoding = Encoding.ASCII;
  42. var cipher = new Arc4Cipher(encoding.GetBytes(key), false);
  43. var cipherText = new byte[] { 0x0A, 0x0f, 0xBB, 0xF3, 0x16, 0xE8, 0xD9, 0x40, 0xAF, 0x0A, 0xD3, 0x0d, 0x05 };
  44. var actualPlainText = cipher.Decrypt(cipherText, 2, cipherText.Length - 4);
  45. Assert.AreEqual(expectedPlainText, encoding.GetString(actualPlainText));
  46. }
  47. [TestMethod]
  48. public void Encrypt_DischargeFirstBytes_False1()
  49. {
  50. const string key = "Key";
  51. const string plainText = "Plaintext";
  52. var encoding = Encoding.ASCII;
  53. var cipher = new Arc4Cipher(encoding.GetBytes(key), false);
  54. var expectedCipherText = new byte[] { 0xBB, 0xF3, 0x16, 0xE8, 0xD9, 0x40, 0xAF, 0x0A, 0xD3 };
  55. var actualCipherText = cipher.Encrypt(encoding.GetBytes(plainText));
  56. Assert.IsNotNull(actualCipherText);
  57. Assert.IsTrue(expectedCipherText.IsEqualTo(actualCipherText));
  58. }
  59. [TestMethod]
  60. public void Encrypt_DischargeFirstBytes_False2()
  61. {
  62. const string key = "Wiki";
  63. const string plainText = "pedia";
  64. var encoding = Encoding.ASCII;
  65. var cipher = new Arc4Cipher(encoding.GetBytes(key), false);
  66. var expectedCipherText = new byte[] { 0x10, 0X21, 0xBF, 0x04, 0x20 };
  67. var actualCipherText = cipher.Encrypt(encoding.GetBytes(plainText));
  68. Assert.IsNotNull(actualCipherText);
  69. Assert.IsTrue(expectedCipherText.IsEqualTo(actualCipherText));
  70. }
  71. [TestMethod]
  72. public void Encrypt_InputAndOffsetAndLength()
  73. {
  74. const string key = "Wiki";
  75. const string plainText = "NOpediaNO";
  76. var encoding = Encoding.ASCII;
  77. var cipher = new Arc4Cipher(encoding.GetBytes(key), false);
  78. var plainTextBytes = encoding.GetBytes(plainText);
  79. var expectedCipherText = new byte[] { 0x10, 0X21, 0xBF, 0x04, 0x20 };
  80. var actualCipherText = cipher.Encrypt(plainTextBytes, 2, plainTextBytes.Length - 4);
  81. Assert.IsNotNull(actualCipherText);
  82. Assert.IsTrue(expectedCipherText.IsEqualTo(actualCipherText));
  83. Assert.IsTrue(plainTextBytes.IsEqualTo(encoding.GetBytes(plainText)));
  84. }
  85. }
  86. }