mcrypt_rijndael128_128BitKey.phpt 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. --TEST--
  2. Test mcrypt_encrypt() function : AES 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. /* Prototype : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv)
  17. * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv
  18. * Source code: ext/mcrypt/mcrypt.c
  19. * Alias to functions:
  20. */
  21. /* Prototype : string mcrypt_cbc(int cipher, string key, string data, int mode, string iv)
  22. * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv
  23. * Source code: ext/mcrypt/mcrypt.c
  24. * Alias to functions:
  25. */
  26. echo "*** Testing mcrypt : Rijndael128 functionality ***\n";
  27. $cipher = MCRYPT_RIJNDAEL_128;
  28. $mode = MCRYPT_MODE_CBC;
  29. $data = b'This is the secret message which must be encrypted';
  30. // keys up to 128 bits (16 bytes)
  31. $keys = array(
  32. null,
  33. '',
  34. b'12345678',
  35. b'1234567890123456'
  36. );
  37. // rijndael128 is a block cipher of 128 bits (16 bytes)
  38. $ivs = array(
  39. null,
  40. '',
  41. b'12345678',
  42. b'1234567890123456',
  43. b'12345678901234567'
  44. );
  45. $iv = b'1234567890123456';
  46. echo "\n--- testing different key lengths\n";
  47. foreach ($keys as $key) {
  48. echo "\nkey length=".strlen($key)."\n";
  49. $res = mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_CBC, $iv);
  50. var_dump(bin2hex($res));
  51. $res = mcrypt_cbc($cipher, $key, $res, MCRYPT_DECRYPT, $iv);
  52. var_dump(bin2hex($res));
  53. }
  54. $key = b'1234567890123456';
  55. echo "\n--- testing different iv lengths\n";
  56. foreach ($ivs as $iv) {
  57. echo "\niv length=".strlen($iv)."\n";
  58. $res = mcrypt_cbc($cipher, $key, $data, $mode, $iv);
  59. var_dump(bin2hex($res));
  60. $res = mcrypt_decrypt($cipher, $key, $res, MCRYPT_MODE_CBC, $iv);
  61. var_dump(bin2hex($res));
  62. }
  63. ?>
  64. ===DONE===
  65. --EXPECTF--
  66. *** Testing mcrypt : Rijndael128 functionality ***
  67. --- testing different key lengths
  68. key length=0
  69. Warning: mcrypt_encrypt(): Key of size 0 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d
  70. string(0) ""
  71. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  72. Warning: mcrypt_cbc(): Key of size 0 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d
  73. string(0) ""
  74. key length=0
  75. Warning: mcrypt_encrypt(): Key of size 0 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d
  76. string(0) ""
  77. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  78. Warning: mcrypt_cbc(): Key of size 0 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d
  79. string(0) ""
  80. key length=8
  81. Warning: mcrypt_encrypt(): Key of size 8 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d
  82. string(0) ""
  83. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  84. Warning: mcrypt_cbc(): Key of size 8 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d
  85. string(0) ""
  86. key length=16
  87. string(128) "dc8f957ec530acf10cd95ba7da7b6405380fe19a2941e9a8de54680512f18491bc374e5464885ae6c2ae2aa7a6cdd2fbe12a06bbc4bd59dbbfaa15f09044f101"
  88. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  89. string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
  90. --- testing different iv lengths
  91. iv length=0
  92. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  93. Warning: mcrypt_cbc(): Received initialization vector of size 0, but size 16 is required for this encryption mode in %s on line %d
  94. string(0) ""
  95. Warning: mcrypt_decrypt(): Received initialization vector of size 0, but size 16 is required for this encryption mode in %s on line %d
  96. string(0) ""
  97. iv length=0
  98. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  99. Warning: mcrypt_cbc(): Received initialization vector of size 0, but size 16 is required for this encryption mode in %s on line %d
  100. string(0) ""
  101. Warning: mcrypt_decrypt(): Received initialization vector of size 0, but size 16 is required for this encryption mode in %s on line %d
  102. string(0) ""
  103. iv length=8
  104. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  105. Warning: mcrypt_cbc(): Received initialization vector of size 8, but size 16 is required for this encryption mode in %s on line %d
  106. string(0) ""
  107. Warning: mcrypt_decrypt(): Received initialization vector of size 8, but size 16 is required for this encryption mode in %s on line %d
  108. string(0) ""
  109. iv length=16
  110. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  111. string(128) "dc8f957ec530acf10cd95ba7da7b6405380fe19a2941e9a8de54680512f18491bc374e5464885ae6c2ae2aa7a6cdd2fbe12a06bbc4bd59dbbfaa15f09044f101"
  112. string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
  113. iv length=17
  114. Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
  115. Warning: mcrypt_cbc(): Received initialization vector of size 17, but size 16 is required for this encryption mode in %s on line %d
  116. string(0) ""
  117. Warning: mcrypt_decrypt(): Received initialization vector of size 17, but size 16 is required for this encryption mode in %s on line %d
  118. string(0) ""
  119. ===DONE===