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 upto 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'1234567890123456';
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
58string(112) "082b437d039d09418e20dc9de1dafa7ed6da5c6335b78950968441da1faf40c1f886e04da8ca177b80b376811e138c1bf51cb48dae2e7939"
59
60key length=20
61string(112) "0627351e0f8a082bf7981ae2c700a43fd3d44b270ac67b00fded1c5796eea935be0fef2a23da0b3f5e243929e62ac957bf0bf463aa90fc4f"
62
63key length=24
64string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac"
65
66key length=26
67
68Warning: mcrypt_cbc(): Size of key is too large for this algorithm in %s on line %d
69string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac"
70
71--- testing different iv lengths
72
73iv length=4
74
75Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d
76string(112) "440a6f54601969b127aad3c217ce7583c7f7b29989693130645569301db0020b29a34a3dcd104b2d0e3ba19d6cbd8a33d352b9c27cc34ef1"
77
78iv length=8
79string(112) "bac347506bf092c5557c4363c301745d78f047028e2953e84fd66b30aeb6005812dadbe8baa871b83278341599b0c448ddaaa52b5a378ce5"
80
81iv length=9
82
83Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d
84string(112) "440a6f54601969b127aad3c217ce7583c7f7b29989693130645569301db0020b29a34a3dcd104b2d0e3ba19d6cbd8a33d352b9c27cc34ef1"
85===DONE===
86