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