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 & ~E_DEPRECATED);
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
19echo "*** Testing mcrypt_ecb() : basic functionality ***\n";
20
21
22$cipher = MCRYPT_TRIPLEDES;
23$data = b"This is the secret message which must be encrypted";
24$mode = MCRYPT_DECRYPT;
25
26// tripledes uses keys up to 192 bits (24 bytes)
27$keys = array(
28   b'12345678',
29   b'12345678901234567890',
30   b'123456789012345678901234',
31   b'12345678901234567890123456'
32);
33$data1 = array(
34   '0D4ArM3ejyhic9rnCcIW9A==',
35   'q0wt1YeOjLpnKm5WsrzKEw==',
36   'zwKEFeqHkhlj+7HZTRA/yA==',
37   'zwKEFeqHkhlj+7HZTRA/yA=='
38);
39// tripledes is a block cipher of 64 bits (8 bytes)
40$ivs = array(
41   b'1234',
42   b'12345678',
43   b'123456789'
44);
45$data2 = array(
46   '+G7nGcWIxigQcJD+2P14HA==',
47   '+G7nGcWIxigQcJD+2P14HA==',
48   '+G7nGcWIxigQcJD+2P14HA=='
49);
50
51$iv = b'12345678';
52echo "\n--- testing different key lengths\n";
53for ($i = 0; $i < sizeof($keys); $i++) {
54   echo "\nkey length=".strlen($keys[$i])."\n";
55   special_var_dump(mcrypt_ecb($cipher, $keys[$i], base64_decode($data1[$i]), $mode, $iv));
56}
57
58$key = b'123456789012345678901234';
59echo "\n--- testing different iv lengths\n";
60for ($i = 0; $i < sizeof($ivs); $i++) {
61   echo "\niv length=".strlen($ivs[$i])."\n";
62   special_var_dump(mcrypt_ecb($cipher, $key, base64_decode($data2[$i]), $mode, $ivs[$i]));
63}
64
65function special_var_dump($str) {
66   var_dump(bin2hex($str));
67}
68?>
69===DONE===
70--EXPECTF--
71*** Testing mcrypt_ecb() : basic functionality ***
72
73--- testing different key lengths
74
75key length=8
76
77Warning: mcrypt_ecb(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
78string(0) ""
79
80key length=20
81
82Warning: mcrypt_ecb(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
83string(0) ""
84
85key length=24
86string(32) "736563726574206d6573736167650000"
87
88key length=26
89
90Warning: mcrypt_ecb(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
91string(0) ""
92
93--- testing different iv lengths
94
95iv length=4
96string(32) "a9298896ed1b7335f8f10f7ff6d7a239"
97
98iv length=8
99string(32) "a9298896ed1b7335f8f10f7ff6d7a239"
100
101iv length=9
102string(32) "a9298896ed1b7335f8f10f7ff6d7a239"
103===DONE===
104