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"
79
80Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
81string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
82
83key length=0
84string(128) "4fbf24aaa789f5194260ade1acd9499402c1845cc517e8fe43cfb5b90a0df294db33ecd1a836c47d6bf6d8600512ba415e17008a1e1991f81056258d82099397"
85
86Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
87string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
88
89key length=8
90string(128) "d6a3042b278fa5816dc6f46152acbe5fd7d1813c3808c27cd969d8e10a64d0238724edfda0322f4512308f22d142df0e92bed861c2b732f7650e234df59183dc"
91
92Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
93string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
94
95key length=16
96string(128) "dc8f957ec530acf10cd95ba7da7b6405380fe19a2941e9a8de54680512f18491bc374e5464885ae6c2ae2aa7a6cdd2fbe12a06bbc4bd59dbbfaa15f09044f101"
97
98Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
99string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
100
101--- testing different iv lengths
102
103iv length=0
104
105Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
106
107Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d
108string(128) "c082b3fabaae4c8c410eb8dba64bae10e48d79b5241fb8f24462cad43bd0b35ad2746b00817e9dcbc636b44df0ec60b46a57e7a310a308a0947724e3817a13b4"
109
110Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d
111string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
112
113iv length=0
114
115Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
116
117Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d
118string(128) "c082b3fabaae4c8c410eb8dba64bae10e48d79b5241fb8f24462cad43bd0b35ad2746b00817e9dcbc636b44df0ec60b46a57e7a310a308a0947724e3817a13b4"
119
120Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d
121string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
122
123iv length=8
124
125Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
126
127Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d
128string(128) "c082b3fabaae4c8c410eb8dba64bae10e48d79b5241fb8f24462cad43bd0b35ad2746b00817e9dcbc636b44df0ec60b46a57e7a310a308a0947724e3817a13b4"
129
130Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d
131string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
132
133iv length=16
134
135Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
136string(128) "dc8f957ec530acf10cd95ba7da7b6405380fe19a2941e9a8de54680512f18491bc374e5464885ae6c2ae2aa7a6cdd2fbe12a06bbc4bd59dbbfaa15f09044f101"
137string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
138
139iv length=17
140
141Deprecated: Function mcrypt_cbc() is deprecated in %s on line %d
142
143Warning: mcrypt_cbc(): The IV parameter must be as long as the blocksize in %s on line %d
144string(128) "c082b3fabaae4c8c410eb8dba64bae10e48d79b5241fb8f24462cad43bd0b35ad2746b00817e9dcbc636b44df0ec60b46a57e7a310a308a0947724e3817a13b4"
145
146Warning: mcrypt_decrypt(): The IV parameter must be as long as the blocksize in %s on line %d
147string(128) "546869732069732074686520736563726574206d657373616765207768696368206d75737420626520656e637279707465640000000000000000000000000000"
148===DONE===
149