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// tripledes is a block cipher of 64 bits (8 bytes)
27$ivs = array(
28   b'1234',
29   b'12345678',
30   b'123456789');
31
32
33$iv = b'12345678';
34echo "\n--- testing different key lengths\n";
35foreach ($keys as $key) {
36   echo "\nkey length=".strlen($key)."\n";
37   var_dump(bin2hex(mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_CBC, $iv)));
38}
39
40$key = b'123456789012345678901234';
41echo "\n--- testing different iv lengths\n";
42foreach ($ivs as $iv) {
43   echo "\niv length=".strlen($iv)."\n";
44   var_dump(bin2hex(mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_CBC, $iv)));
45}
46?>
47===DONE===
48--EXPECTF--
49--- testing different key lengths
50
51key length=8
52
53Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 28
54
55Warning: mcrypt_encrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
56string(0) ""
57
58key length=20
59
60Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 28
61
62Warning: mcrypt_encrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
63string(0) ""
64
65key length=24
66
67Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 28
68string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac"
69
70key length=26
71
72Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 28
73
74Warning: mcrypt_encrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
75string(0) ""
76
77--- testing different iv lengths
78
79iv length=4
80
81Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 35
82
83Warning: mcrypt_encrypt(): Received initialization vector of size 4, but size 8 is required for this encryption mode in %s on line %d
84string(0) ""
85
86iv length=8
87
88Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 35
89string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac"
90
91iv length=9
92
93Deprecated: Function mcrypt_encrypt() is deprecated in %s%emcrypt_cbc_3des_encrypt.php on line 35
94
95Warning: mcrypt_encrypt(): Received initialization vector of size 9, but size 8 is required for this encryption mode in %s on line %d
96string(0) ""
97===DONE===
98