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 upto 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'1234567890123456';
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
76string(32) "736563726574206d6573736167650000"
77
78key length=20
79string(32) "736563726574206d6573736167650000"
80
81key length=24
82string(32) "736563726574206d6573736167650000"
83
84key length=26
85
86Warning: mcrypt_ecb(): Size of key is too large for this algorithm in %s on line %d
87string(32) "736563726574206d6573736167650000"
88
89--- testing different iv lengths
90
91iv length=4
92string(32) "736563726574206d6573736167650000"
93
94iv length=8
95string(32) "736563726574206d6573736167650000"
96
97iv length=9
98string(32) "736563726574206d6573736167650000"
99===DONE===
100