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}
57
58$key = b'1234567890123456';
59echo "\n--- testing different iv lengths\n";
60foreach ($ivs as $iv) {
61   echo "\niv length=".strlen($iv)."\n";
62   $res = mcrypt_decrypt($cipher, $key, $res, MCRYPT_MODE_CBC, $iv);
63   var_dump(bin2hex($res));
64}
65
66?>
67===DONE===
68--EXPECTF--
69*** Testing mcrypt : Rijndael128 functionality ***
70
71--- testing different key lengths
72
73key length=0
74
75Warning: 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
76string(0) ""
77
78key length=0
79
80Warning: 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
81string(0) ""
82
83key length=8
84
85Warning: 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
86string(0) ""
87
88key length=16
89string(128) "dc8f957ec530acf10cd95ba7da7b6405380fe19a2941e9a8de54680512f18491bc374e5464885ae6c2ae2aa7a6cdd2fbe12a06bbc4bd59dbbfaa15f09044f101"
90
91--- testing different iv lengths
92
93iv length=0
94
95Warning: mcrypt_decrypt(): Received initialization vector of size 0, but size 16 is required for this encryption mode in %s on line %d
96string(0) ""
97
98iv length=0
99
100Warning: mcrypt_decrypt(): Received initialization vector of size 0, but size 16 is required for this encryption mode in %s on line %d
101string(0) ""
102
103iv length=8
104
105Warning: mcrypt_decrypt(): Received initialization vector of size 8, but size 16 is required for this encryption mode in %s on line %d
106string(0) ""
107
108iv length=16
109string(32) "42adc8c0db19473f2c684ff2d6e828a5"
110
111iv length=17
112
113Warning: mcrypt_decrypt(): Received initialization vector of size 17, but size 16 is required for this encryption mode in %s on line %d
114string(0) ""
115===DONE===
116