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