mcrypt_encrypt_3des_ecb.phpt 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. --TEST--
  2. Test mcrypt_encrypt() function : TripleDES 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_encrypt(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_encrypt() : TripleDES functionality ***\n";
  17. $cipher = MCRYPT_TRIPLEDES;
  18. $mode = MCRYPT_MODE_ECB;
  19. $data = b'This is the secret message which must be encrypted';
  20. // tripledes uses keys up to 192 bits (24 bytes)
  21. $keys = array(
  22. b'12345678',
  23. b'12345678901234567890',
  24. b'123456789012345678901234',
  25. b'12345678901234567890123456'
  26. );
  27. echo "\n--- testing different key lengths\n";
  28. foreach ($keys as $key) {
  29. echo "\nkey length=".strlen($key)."\n";
  30. var_dump(bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode)));
  31. }
  32. $key = b'123456789012345678901234';
  33. $ivs = array(
  34. b'1234',
  35. b'12345678',
  36. b'123456789'
  37. );
  38. // ivs should be ignored in ecb mode
  39. echo "\n--- testing different iv lengths\n";
  40. foreach ($ivs as $iv) {
  41. echo "\niv length=".strlen($iv)."\n";
  42. var_dump(bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode, $iv)));
  43. }
  44. ?>
  45. ===DONE===
  46. --EXPECTF--
  47. *** Testing mcrypt_encrypt() : TripleDES functionality ***
  48. --- testing different key lengths
  49. key length=8
  50. Warning: mcrypt_encrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
  51. string(0) ""
  52. key length=20
  53. Warning: mcrypt_encrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
  54. string(0) ""
  55. key length=24
  56. string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27"
  57. key length=26
  58. Warning: mcrypt_encrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
  59. string(0) ""
  60. --- testing different iv lengths
  61. iv length=4
  62. string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27"
  63. iv length=8
  64. string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27"
  65. iv length=9
  66. string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27"
  67. ===DONE===