1--TEST--
2Test mcrypt_encrypt() function : AES 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_encrypt(string cipher, string key, string data, string mode, string iv)
12 * Description: OFB 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 /* Prototype  : string mcrypt_decrypt(string cipher, string key, string data, string mode, string iv)
17 * Description: OFB crypt/decrypt data using key key with cipher cipher starting with iv
18 * Source code: ext/mcrypt/mcrypt.c
19 * Alias to functions:
20 */
21 /* Prototype  : string mcrypt_cbc(int cipher, string key, string data, int mode, string iv)
22 * Description: CBC crypt/decrypt data using key key with cipher cipher starting with iv
23 * Source code: ext/mcrypt/mcrypt.c
24 * Alias to functions:
25 */
26
27echo "*** Testing mcrypt : Rijndael128 functionality ***\n";
28
29$cipher = MCRYPT_RIJNDAEL_128;
30$mode = MCRYPT_MODE_CBC;
31$data = b'This is the secret message which must be encrypted';
32
33// keys up to 128 bits (16 bytes)
34$keys = array(
35   null,
36   '',
37   b'12345678',
38   b'1234567890123456'
39);
40// rijndael128 is a block cipher of 128 bits (16 bytes)
41$ivs = array(
42   null,
43   '',
44   b'12345678',
45   b'1234567890123456',
46   b'12345678901234567'
47);
48
49
50$iv = b'1234567890123456';
51echo "\n--- testing different key lengths\n";
52foreach ($keys as $key) {
53   echo "\nkey length=".strlen($key)."\n";
54   $res = mcrypt_encrypt($cipher, $key, $data, MCRYPT_MODE_CBC, $iv);
55   var_dump(bin2hex($res));
56   $res = mcrypt_cbc($cipher, $key, $res, MCRYPT_DECRYPT, $iv);
57   var_dump(bin2hex($res));
58}
59
60$key = b'1234567890123456';
61echo "\n--- testing different iv lengths\n";
62foreach ($ivs as $iv) {
63   echo "\niv length=".strlen($iv)."\n";
64   $res = mcrypt_cbc($cipher, $key, $data, $mode, $iv);
65   var_dump(bin2hex($res));
66   $res = mcrypt_decrypt($cipher, $key, $res, MCRYPT_MODE_CBC, $iv);
67   var_dump(bin2hex($res));
68}
69
70?>
71===DONE===
72--EXPECTF--
73*** Testing mcrypt : Rijndael128 functionality ***
74
75--- testing different key lengths
76
77key length=0
78
79Warning: mcrypt_encrypt(): Key of size 0 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d
80string(0) ""
81
82Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
83
84Warning: mcrypt_cbc(): Key of size 0 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d
85string(0) ""
86
87key length=0
88
89Warning: mcrypt_encrypt(): Key of size 0 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d
90string(0) ""
91
92Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
93
94Warning: mcrypt_cbc(): Key of size 0 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d
95string(0) ""
96
97key length=8
98
99Warning: mcrypt_encrypt(): Key of size 8 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d
100string(0) ""
101
102Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
103
104Warning: mcrypt_cbc(): Key of size 8 not supported by this algorithm. Only keys of sizes 16, 24 or 32 supported in %s on line %d
105string(0) ""
106
107key length=16
108string(128) "dc8f957ec530acf10cd95ba7da7b6405380fe19a2941e9a8de54680512f18491bc374e5464885ae6c2ae2aa7a6cdd2fbe12a06bbc4bd59dbbfaa15f09044f101"
109
110Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
111string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
112
113--- testing different iv lengths
114
115iv length=0
116
117Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
118
119Warning: mcrypt_cbc(): Received initialization vector of size 0, but size 16 is required for this encryption mode in %s on line %d
120string(0) ""
121
122Warning: mcrypt_decrypt(): Received initialization vector of size 0, but size 16 is required for this encryption mode in %s on line %d
123string(0) ""
124
125iv length=0
126
127Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
128
129Warning: mcrypt_cbc(): Received initialization vector of size 0, but size 16 is required for this encryption mode in %s on line %d
130string(0) ""
131
132Warning: mcrypt_decrypt(): Received initialization vector of size 0, but size 16 is required for this encryption mode in %s on line %d
133string(0) ""
134
135iv length=8
136
137Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
138
139Warning: mcrypt_cbc(): Received initialization vector of size 8, but size 16 is required for this encryption mode in %s on line %d
140string(0) ""
141
142Warning: mcrypt_decrypt(): Received initialization vector of size 8, but size 16 is required for this encryption mode in %s on line %d
143string(0) ""
144
145iv length=16
146
147Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
148string(128) "dc8f957ec530acf10cd95ba7da7b6405380fe19a2941e9a8de54680512f18491bc374e5464885ae6c2ae2aa7a6cdd2fbe12a06bbc4bd59dbbfaa15f09044f101"
149string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
150
151iv length=17
152
153Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
154
155Warning: mcrypt_cbc(): Received initialization vector of size 17, but size 16 is required for this encryption mode in %s on line %d
156string(0) ""
157
158Warning: mcrypt_decrypt(): Received initialization vector of size 17, but size 16 is required for this encryption mode in %s on line %d
159string(0) ""
160===DONE===
161