RC2Test.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // TestSuite.System.Security.Cryptography.RC2Test.cs
  3. //
  4. // Author:
  5. // Andrew Birkett ([email protected])
  6. //
  7. using System;
  8. using System.Security.Cryptography;
  9. using NUnit.Framework;
  10. namespace MonoTests.System.Security.Cryptography {
  11. public class RC2Test : TestCase {
  12. private void CheckECB(int effective_bits, byte[] key, byte[] pt, byte[] expected)
  13. {
  14. RC2 c = RC2.Create();
  15. c.Mode = CipherMode.ECB;
  16. c.Padding = PaddingMode.Zeros;
  17. c.Key = key;
  18. c.KeySize = key.Length * 8;
  19. c.EffectiveKeySize = effective_bits;
  20. byte[] iv = new byte[pt.Length];
  21. for (int i=0; i < pt.Length; i++) iv[i] = 0;
  22. ICryptoTransform encryptor = c.CreateEncryptor(key, iv);
  23. ICryptoTransform decryptor = c.CreateDecryptor(key, iv);
  24. byte[] ct = new byte[pt.Length];
  25. int n = encryptor.TransformBlock(pt, 0, pt.Length, ct, 0);
  26. AssertEquals("EncryptLen", n, pt.Length);
  27. for (int i=0; i < n; i++) {
  28. AssertEquals("Encrypt" + i, ct[i], expected[i]);
  29. }
  30. byte[] rt = new byte[ct.Length];
  31. n = decryptor.TransformBlock(ct, 0, ct.Length, rt, 0);
  32. AssertEquals("DecryptLen", n, ct.Length);
  33. for (int i=0; i < n; i++) {
  34. AssertEquals("Decrypt" + i, rt[i], pt[i]);
  35. }
  36. }
  37. public void TestECBVectors()
  38. {
  39. {
  40. byte[] key = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  41. byte[] pt = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
  42. byte[] ct = { 0x27, 0x8b, 0x27, 0xe4, 0x2e, 0x2f, 0x0d, 0x49 };
  43. CheckECB(64, key, pt, ct);
  44. }
  45. {
  46. byte[] key = { 0x30, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  47. byte[] pt = { 0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 };
  48. byte[] ct = { 0x30, 0x64, 0x9e, 0xdf, 0x9b, 0xe7, 0xd2, 0xc2 };
  49. CheckECB(64, key, pt, ct);
  50. }
  51. {
  52. byte[] key = { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f,
  53. 0x0f, 0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2 };
  54. byte[] pt = { 0, 0, 0, 0, 0, 0, 0, 0 };
  55. byte[] ct = { 0x22, 0x69, 0x55, 0x2a, 0xb0, 0xf8, 0x5c, 0xa6 };
  56. CheckECB(128, key, pt, ct);
  57. }
  58. // The following tests work fine with mono corlib, but the MS
  59. // corlib doesn't support keys bigger than 128 or smaller than 40
  60. // bits, and it doesn't supports effective key sizes which differ
  61. // from the key size. So, mono does RC2 better than MS!
  62. // {
  63. // byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 };
  64. // byte[] pt = { 0, 0, 0, 0, 0, 0, 0, 0 };
  65. // byte[] ct = { 0xeb, 0xb7, 0x73, 0xf9, 0x93, 0x27, 0x8e, 0xff };
  66. // CheckECB(63, key, pt, ct);
  67. // }
  68. // {
  69. // byte[] key = { 0x88 };
  70. // byte[] pt = { 0, 0, 0, 0, 0, 0, 0, 0 };
  71. // byte[] ct = { 0x61, 0xa8, 0xa2, 0x44, 0xad, 0xac, 0xcc, 0xf0 };
  72. // CheckECB(64, key, pt, ct);
  73. // }
  74. // {
  75. // byte[] key = { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a };
  76. // byte[] pt = { 0, 0, 0, 0, 0, 0, 0, 0 };
  77. // byte[] ct = { 0x6c, 0xcf, 0x43, 0x08, 0x97, 0x4c, 0x26, 0x7f };
  78. // CheckECB(64, key, pt, ct);
  79. // }
  80. // {
  81. // byte[] key = { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f,
  82. // 0x0f, 0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2 };
  83. // byte[] pt = { 0, 0, 0, 0, 0, 0, 0, 0 };
  84. // byte[] ct = { 0x1a, 0x80, 0x7d, 0x27, 0x2b, 0xbe, 0x5d, 0xb1 };
  85. // CheckECB(64, key, pt, ct);
  86. // }
  87. // {
  88. // byte[] key = { 0x88, 0xbc, 0xa9, 0x0e, 0x90, 0x87, 0x5a, 0x7f,
  89. // 0x0f, 0x79, 0xc3, 0x84, 0x62, 0x7b, 0xaf, 0xb2,
  90. // 0x16, 0xf8, 0x0a, 0x6f, 0x85, 0x92, 0x05, 0x84,
  91. // 0xc4, 0x2f, 0xce, 0xb0, 0xbe, 0x25, 0x5d, 0xaf, 0x1e };
  92. // byte[] pt = { 0, 0, 0, 0, 0, 0, 0, 0 };
  93. // byte[] ct = { 0x5b, 0x78, 0xd3, 0xa4, 0x3d, 0xff, 0xf1, 0xf1 };
  94. // CheckECB(129, key, pt, ct);
  95. // }
  96. }
  97. }
  98. }