/MicroFrameworkPK_v4_2/Test/Platform/Tests/CLR/System/Security/STREAMS/Desktop/RijndKnownEnc1.cs

https://bitbucket.org/pmfsampaio/netmf-lpc · C# · 142 lines · 111 code · 26 blank · 5 comment · 20 complexity · 36e3d6c28ff3bed0f86c2771b81b6b09 MD5 · raw file

  1. //
  2. // Rijndael Encrypting Comparison with known vectors in ECB mode.
  3. //
  4. using System;
  5. using System.Security.Cryptography;
  6. using System.IO;
  7. class RijndaelKnown1
  8. {
  9. static void PrintByteArray(Byte[] arr)
  10. {
  11. if (arr==null) {
  12. Console.WriteLine("null");
  13. return;
  14. }
  15. int i;
  16. for (i=0; i<arr.Length; i++) {
  17. Console.Write("{0:X} ", arr[i]);
  18. if ( (i+9)%8 == 0 ) Console.WriteLine();
  19. }
  20. if (i%8 != 0) Console.WriteLine();
  21. }
  22. static Boolean Compare(Byte[] rgb1, Byte[] rgb2) {
  23. int i;
  24. if (rgb1.Length != rgb2.Length) return false;
  25. for (i=0; i<rgb1.Length; i++) {
  26. if (rgb1[i] != rgb2[i]) return false;
  27. }
  28. return true;
  29. }
  30. static Boolean TestKnownEnc(Byte[] Key, Byte[] IV, Byte[] Plain, Byte[] Cipher, CipherMode mode, PaddingMode padding)
  31. {
  32. Byte[] CipherCalculated;
  33. Console.WriteLine("Encrypting the following bytes:");
  34. PrintByteArray(Plain);
  35. Console.WriteLine("With the following Key:");
  36. PrintByteArray(Key);
  37. Console.WriteLine("and IV:");
  38. PrintByteArray(IV);
  39. Console.WriteLine("mode: " + mode + " padding: " + padding);
  40. Console.WriteLine("Expecting this ciphertext:");
  41. PrintByteArray(Cipher);
  42. RijndaelManaged rjnd = new RijndaelManaged();
  43. rjnd.Mode = mode;
  44. rjnd.Padding = padding;
  45. // rjnd.BlockSize = 128;
  46. // rjnd.KeySize = 128;
  47. ICryptoTransform sse = rjnd.CreateEncryptor(Key, IV);
  48. MemoryStream ms = new MemoryStream();
  49. CryptoStream cs = new CryptoStream(ms, sse, CryptoStreamMode.Write);
  50. cs.Write(Plain,0,Plain.Length);
  51. cs.FlushFinalBlock();
  52. CipherCalculated = ms.ToArray();
  53. cs.Close();
  54. Console.WriteLine("Computed this cyphertext:");
  55. PrintByteArray(CipherCalculated);
  56. if (!Compare(Cipher, CipherCalculated)) {
  57. Console.WriteLine("ERROR: result is different from the expected");
  58. return false;
  59. }
  60. Console.WriteLine("OK");
  61. return true;
  62. }
  63. public static Boolean Test()
  64. {
  65. Boolean bRes = true;
  66. Byte[] Key = {0x00,0x01,0x02,0x03,0x05,0x06,0x07,0x08,0x0A,0x0B,0x0C,0x0D,0x0F,0x10,0x11,0x12};
  67. Byte[] IV = null;
  68. Byte[] Plain = {0x50,0x68,0x12,0xA4,0x5F,0x08,0xC8,0x89,0xB9,0x7F,0x59,0x80,0x03,0x8B,0x83,0x59};
  69. Byte[] Cipher= {0xD8,0xF5,0x32,0x53,0x82,0x89,0xEF,0x7D,0x06,0xB5,0x06,0xA4,0xFD,0x5B,0xE9,0xC9};
  70. bRes = TestKnownEnc(Key, IV, Plain, Cipher, CipherMode.ECB, PaddingMode.None) && bRes;
  71. Byte[] Key1 = {0x00,0x01,0x02,0x03,0x05,0x06,0x07,0x08,0x0A,0x0B,0x0C,0x0D,0x0F,0x10,0x11,0x12,0x14,0x15,0x16,0x17,0x19,0x1A,0x1B,0x1C,0x1E,0x1F,0x20,0x21,0x23,0x24,0x25,0x26};
  72. Byte[] IV1 = null;
  73. Byte[] Plain1 = {0x83,0x4E,0xAD,0xFC,0xCA,0xC7,0xE1,0xB3,0x06,0x64,0xB1,0xAB,0xA4,0x48,0x15,0xAB};
  74. Byte[] Cipher1= {0x19,0x46,0xDA,0xBF,0x6A,0x03,0xA2,0xA2,0xC3,0xD0,0xB0,0x50,0x80,0xAE,0xD6,0xFC};
  75. bRes = TestKnownEnc(Key1, IV1, Plain1, Cipher1, CipherMode.ECB, PaddingMode.None) && bRes;
  76. Byte[] Key2 = {0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  77. Byte[] IV2 = null;
  78. Byte[] Plain2 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  79. Byte[] Cipher2= {0x0E,0xDD,0x33,0xD3,0xC6,0x21,0xE5,0x46,0x45,0x5B,0xD8,0xBA,0x14,0x18,0xBE,0xC8};
  80. bRes = TestKnownEnc(Key2, IV2, Plain2, Cipher2, CipherMode.ECB, PaddingMode.None) && bRes;
  81. Byte[] Key3 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  82. Byte[] IV3 = null;
  83. Byte[] Plain3 = {0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  84. Byte[] Cipher3= {0x3A,0xD7,0x8E,0x72,0x6C,0x1E,0xC0,0x2B,0x7E,0xBF,0xE9,0x2B,0x23,0xD9,0xEC,0x34};
  85. bRes = TestKnownEnc(Key3, IV3, Plain3, Cipher3, CipherMode.ECB, PaddingMode.None) && bRes;
  86. Byte[] Key4 = {0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  87. Byte[] IV4 = null;
  88. Byte[] Plain4 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  89. Byte[] Cipher4= {0xDE,0x88,0x5D,0xC8,0x7F,0x5A,0x92,0x59,0x40,0x82,0xD0,0x2C,0xC1,0xE1,0xB4,0x2C};
  90. bRes = TestKnownEnc(Key4, IV4, Plain4, Cipher4, CipherMode.ECB, PaddingMode.None) && bRes;
  91. Byte[] Key5 = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  92. Byte[] IV5 = null;
  93. Byte[] Plain5 = {0x80,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  94. Byte[] Cipher5= {0x6C,0xD0,0x25,0x13,0xE8,0xD4,0xDC,0x98,0x6B,0x4A,0xFE,0x08,0x7A,0x60,0xBD,0x0C};
  95. bRes = TestKnownEnc(Key5, IV5, Plain5, Cipher5, CipherMode.ECB, PaddingMode.None) && bRes;
  96. return bRes;
  97. }
  98. public static void Main(String[] args)
  99. {
  100. try {
  101. if (Test())
  102. {
  103. Console.WriteLine("PASSED");
  104. Environment.ExitCode = (100);
  105. } else {
  106. Console.WriteLine("FAILED");
  107. Environment.ExitCode = (123);
  108. }
  109. }
  110. catch(Exception e) {
  111. Console.Write("Exception: {0}", e.ToString());
  112. Environment.ExitCode = (123);
  113. }
  114. return;
  115. }
  116. }