mcrypt_decrypt_3des_ecb.phpt 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. --TEST--
  2. Test mcrypt_decrypt() 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. /* Prototype : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv)
  12. * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv
  13. * Source code: ext/mcrypt/mcrypt.c
  14. * Alias to functions:
  15. */
  16. echo "*** Testing mcrypt_decrypt() : basic functionality ***\n";
  17. // Initialise all required variables
  18. $cipher = MCRYPT_3DES;
  19. $mode = MCRYPT_MODE_ECB;
  20. // tripledes uses keys with exactly 192 bits (24 bytes)
  21. $keys = array(
  22. b'12345678',
  23. b'12345678901234567890',
  24. b'123456789012345678901234',
  25. b'12345678901234567890123456'
  26. );
  27. $data1 = array(
  28. '0D4ArM3ejyhic9rnCcIW9A==',
  29. 'q0wt1YeOjLpnKm5WsrzKEw==',
  30. 'zwKEFeqHkhlj+7HZTRA/yA==',
  31. 'zwKEFeqHkhlj+7HZTRA/yA=='
  32. );
  33. // tripledes is a block cipher of 64 bits (8 bytes)
  34. $ivs = array(
  35. b'1234',
  36. b'12345678',
  37. b'123456789'
  38. );
  39. $data2 = array(
  40. '+G7nGcWIxigQcJD+2P14HA==',
  41. '+G7nGcWIxigQcJD+2P14HA==',
  42. '+G7nGcWIxigQcJD+2P14HA=='
  43. );
  44. echo "\n--- testing different key lengths\n";
  45. for ($i = 0; $i < sizeof($keys); $i++) {
  46. echo "\nkey length=".strlen($keys[$i])."\n";
  47. special_var_dump(mcrypt_decrypt($cipher, $keys[$i], base64_decode($data1[$i]), $mode));
  48. }
  49. $key = b'123456789012345678901234';
  50. echo "\n--- testing different iv lengths\n";
  51. for ($i = 0; $i < sizeof($ivs); $i++) {
  52. echo "\niv length=".strlen($ivs[$i])."\n";
  53. special_var_dump(mcrypt_decrypt($cipher, $key, base64_decode($data2[$i]), $mode, $ivs[$i]));
  54. }
  55. function special_var_dump($str) {
  56. var_dump(bin2hex($str));
  57. }
  58. ?>
  59. ===DONE===
  60. --EXPECTF--
  61. *** Testing mcrypt_decrypt() : basic functionality ***
  62. --- testing different key lengths
  63. key length=8
  64. Warning: mcrypt_decrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
  65. string(0) ""
  66. key length=20
  67. Warning: mcrypt_decrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
  68. string(0) ""
  69. key length=24
  70. string(32) "736563726574206d6573736167650000"
  71. key length=26
  72. Warning: mcrypt_decrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
  73. string(0) ""
  74. --- testing different iv lengths
  75. iv length=4
  76. string(32) "a9298896ed1b7335f8f10f7ff6d7a239"
  77. iv length=8
  78. string(32) "a9298896ed1b7335f8f10f7ff6d7a239"
  79. iv length=9
  80. string(32) "a9298896ed1b7335f8f10f7ff6d7a239"
  81. ===DONE===