mcrypt_cbc_3des_decrypt.phpt 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /* Prototype : string mcrypt_cbc(int cipher, string key, string data, int mode, string iv)
  12. * Description: CBC 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_cbc() : basic functionality ***\n";
  17. $cipher = MCRYPT_TRIPLEDES;
  18. $data = b"This is the secret message which must be encrypted";
  19. $mode = MCRYPT_DECRYPT;
  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. 'IleMhoxiOthmHua4tFBHOw==',
  29. 'EeF1s6C+w1IiHj1gdDn81g==',
  30. 'EEuXpjZPueyYoG0LGQ199Q==',
  31. 'EEuXpjZPueyYoG0LGQ199Q=='
  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. // data represented in base64 (ascii)
  40. $data2 = array(
  41. '+G7nGcWIxij3TZjpI9lJdQ==',
  42. '3bJiFMeyScxOLQcE6mZtLg==',
  43. '+G7nGcWIxij3TZjpI9lJdQ=='
  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_cbc($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_cbc($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_cbc() : basic functionality ***
  64. --- testing different key lengths
  65. key length=8
  66. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  67. Warning: mcrypt_cbc(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
  68. string(0) ""
  69. key length=20
  70. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  71. Warning: mcrypt_cbc(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
  72. string(0) ""
  73. key length=24
  74. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  75. string(32) "736563726574206d6573736167650000"
  76. key length=26
  77. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  78. Warning: mcrypt_cbc(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
  79. string(0) ""
  80. --- testing different iv lengths
  81. iv length=4
  82. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  83. Warning: mcrypt_cbc(): Received initialization vector of size 4, but size 8 is required for this encryption mode in %s on line %d
  84. string(0) ""
  85. iv length=8
  86. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  87. string(32) "659ec947f4dc3a3b9c50de744598d3c8"
  88. iv length=9
  89. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  90. Warning: mcrypt_cbc(): Received initialization vector of size 9, but size 8 is required for this encryption mode in %s on line %d
  91. string(0) ""
  92. ===DONE===