1--TEST--
2Test mcrypt_encrypt() function : TripleDES 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 upto 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
78string(128) "4fbf24aaa789f5194260ade1acd9499402c1845cc517e8fe43cfb5b90a0df294db33ecd1a836c47d6bf6d8600512ba415e17008a1e1991f81056258d82099397"
79string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
80
81key length=0
82string(128) "4fbf24aaa789f5194260ade1acd9499402c1845cc517e8fe43cfb5b90a0df294db33ecd1a836c47d6bf6d8600512ba415e17008a1e1991f81056258d82099397"
83string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
84
85key length=8
86string(128) "d6a3042b278fa5816dc6f46152acbe5fd7d1813c3808c27cd969d8e10a64d0238724edfda0322f4512308f22d142df0e92bed861c2b732f7650e234df59183dc"
87string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
88
89key length=16
90string(128) "dc8f957ec530acf10cd95ba7da7b6405380fe19a2941e9a8de54680512f18491bc374e5464885ae6c2ae2aa7a6cdd2fbe12a06bbc4bd59dbbfaa15f09044f101"
91string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
92
93--- testing different iv lengths
94
95iv length=0
96
97Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d
98string(128) "c082b3fabaae4c8c410eb8dba64bae10e48d79b5241fb8f24462cad43bd0b35ad2746b00817e9dcbc636b44df0ec60b46a57e7a310a308a0947724e3817a13b4"
99
100Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d
101string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
102
103iv length=0
104
105Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d
106string(128) "c082b3fabaae4c8c410eb8dba64bae10e48d79b5241fb8f24462cad43bd0b35ad2746b00817e9dcbc636b44df0ec60b46a57e7a310a308a0947724e3817a13b4"
107
108Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d
109string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
110
111iv length=8
112
113Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d
114string(128) "c082b3fabaae4c8c410eb8dba64bae10e48d79b5241fb8f24462cad43bd0b35ad2746b00817e9dcbc636b44df0ec60b46a57e7a310a308a0947724e3817a13b4"
115
116Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d
117string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
118
119iv length=16
120string(128) "dc8f957ec530acf10cd95ba7da7b6405380fe19a2941e9a8de54680512f18491bc374e5464885ae6c2ae2aa7a6cdd2fbe12a06bbc4bd59dbbfaa15f09044f101"
121string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
122
123iv length=17
124
125Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d
126string(128) "c082b3fabaae4c8c410eb8dba64bae10e48d79b5241fb8f24462cad43bd0b35ad2746b00817e9dcbc636b44df0ec60b46a57e7a310a308a0947724e3817a13b4"
127
128Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d
129string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
130===DONE===