mcrypt_ecb_3des_decrypt.phpt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. --TEST--
  2. Test mcrypt_cbc() function : basic functionality
  3. --SKIPIF--
  4. <?php
  5. if (!extension_loaded("mcrypt")) {
  6. print "skip - mcrypt extension not loaded";
  7. }
  8. ?>
  9. --FILE--
  10. <?php
  11. error_reporting(E_ALL & ~E_DEPRECATED);
  12. /* Prototype : string mcrypt_ecb(int cipher, string key, string data, int mode, string iv)
  13. * Description: ECB crypt/decrypt data using key key with cipher cipher starting with iv
  14. * Source code: ext/mcrypt/mcrypt.c
  15. * Alias to functions:
  16. */
  17. echo "*** Testing mcrypt_ecb() : basic functionality ***\n";
  18. $cipher = MCRYPT_TRIPLEDES;
  19. $data = b"This is the secret message which must be encrypted";
  20. $mode = MCRYPT_DECRYPT;
  21. // tripledes uses keys up to 192 bits (24 bytes)
  22. $keys = array(
  23. b'12345678',
  24. b'12345678901234567890',
  25. b'123456789012345678901234',
  26. b'12345678901234567890123456'
  27. );
  28. $data1 = array(
  29. '0D4ArM3ejyhic9rnCcIW9A==',
  30. 'q0wt1YeOjLpnKm5WsrzKEw==',
  31. 'zwKEFeqHkhlj+7HZTRA/yA==',
  32. 'zwKEFeqHkhlj+7HZTRA/yA=='
  33. );
  34. // tripledes is a block cipher of 64 bits (8 bytes)
  35. $ivs = array(
  36. b'1234',
  37. b'12345678',
  38. b'123456789'
  39. );
  40. $data2 = array(
  41. '+G7nGcWIxigQcJD+2P14HA==',
  42. '+G7nGcWIxigQcJD+2P14HA==',
  43. '+G7nGcWIxigQcJD+2P14HA=='
  44. );
  45. $iv = b'12345678';
  46. echo "\n--- testing different key lengths\n";
  47. for ($i = 0; $i < sizeof($keys); $i++) {
  48. echo "\nkey length=".strlen($keys[$i])."\n";
  49. special_var_dump(mcrypt_ecb($cipher, $keys[$i], base64_decode($data1[$i]), $mode, $iv));
  50. }
  51. $key = b'123456789012345678901234';
  52. echo "\n--- testing different iv lengths\n";
  53. for ($i = 0; $i < sizeof($ivs); $i++) {
  54. echo "\niv length=".strlen($ivs[$i])."\n";
  55. special_var_dump(mcrypt_ecb($cipher, $key, base64_decode($data2[$i]), $mode, $ivs[$i]));
  56. }
  57. function special_var_dump($str) {
  58. var_dump(bin2hex($str));
  59. }
  60. ?>
  61. ===DONE===
  62. --EXPECTF--
  63. *** Testing mcrypt_ecb() : basic functionality ***
  64. --- testing different key lengths
  65. key length=8
  66. Warning: mcrypt_ecb(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
  67. string(0) ""
  68. key length=20
  69. Warning: mcrypt_ecb(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
  70. string(0) ""
  71. key length=24
  72. string(32) "736563726574206d6573736167650000"
  73. key length=26
  74. Warning: mcrypt_ecb(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
  75. string(0) ""
  76. --- testing different iv lengths
  77. iv length=4
  78. string(32) "a9298896ed1b7335f8f10f7ff6d7a239"
  79. iv length=8
  80. string(32) "a9298896ed1b7335f8f10f7ff6d7a239"
  81. iv length=9
  82. string(32) "a9298896ed1b7335f8f10f7ff6d7a239"
  83. ===DONE===