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
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
17$cipher = MCRYPT_TRIPLEDES;
18$data = b"This is the secret message which must be encrypted";
19
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
46$iv = b'12345678';
47echo "\n--- testing different key lengths\n";
48for ($i = 0; $i < sizeof($keys); $i++) {
49   echo "\nkey length=".strlen($keys[$i])."\n";
50   special_var_dump(mcrypt_decrypt($cipher, $keys[$i], base64_decode($data1[$i]), MCRYPT_MODE_CBC, $iv));
51}
52
53$key = b'123456789012345678901234';
54echo "\n--- testing different iv lengths\n";
55for ($i = 0; $i < sizeof($ivs); $i++) {
56   echo "\niv length=".strlen($ivs[$i])."\n";
57   special_var_dump(mcrypt_decrypt($cipher, $key, base64_decode($data2[$i]), MCRYPT_MODE_CBC, $ivs[$i]));
58}
59
60function special_var_dump($str) {
61   var_dump(bin2hex($str));
62}
63?>
64===DONE===
65--EXPECTF--
66--- testing different key lengths
67
68key length=8
69
70Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 41
71
72Warning: mcrypt_decrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
73string(0) ""
74
75key length=20
76
77Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 41
78
79Warning: mcrypt_decrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
80string(0) ""
81
82key length=24
83
84Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 41
85string(32) "736563726574206d6573736167650000"
86
87key length=26
88
89Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 41
90
91Warning: mcrypt_decrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
92string(0) ""
93
94--- testing different iv lengths
95
96iv length=4
97
98Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 48
99
100Warning: mcrypt_decrypt(): Received initialization vector of size 4, but size 8 is required for this encryption mode in %s on line %d
101string(0) ""
102
103iv length=8
104
105Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 48
106string(32) "659ec947f4dc3a3b9c50de744598d3c8"
107
108iv length=9
109
110Deprecated: Function mcrypt_decrypt() is deprecated in %s%emcrypt_cbc_3des_decrypt.php on line 48
111
112Warning: mcrypt_decrypt(): Received initialization vector of size 9, but size 8 is required for this encryption mode in %s on line %d
113string(0) ""
114===DONE===
115