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
17echo "*** Testing mcrypt_encrypt() : TripleDES functionality ***\n";
18
19//test CBC, ECB modes
20//test encrypt decrypt
21//test tripledes, aes
22//test different lengths of key, iv
23//test no iv being passed on CBC, ECB
24//test up to 32 bytes with unlimited strength
25
26$cipher = MCRYPT_TRIPLEDES;
27$mode = MCRYPT_MODE_CBC;
28$data = b'This is the secret message which must be encrypted';
29
30// tripledes uses keys up to 192 bits (24 bytes)
31$keys = array(
32   b'12345678',
33   b'12345678901234567890',
34   b'123456789012345678901234',
35   b'12345678901234567890123456'
36);
37// tripledes is a block cipher of 64 bits (8 bytes)
38$ivs = array(
39   b'1234',
40   b'12345678',
41   b'123456789'
42);
43
44
45$iv = b'12345678';
46echo "\n--- testing different key lengths\n";
47foreach ($keys as $key) {
48   echo "\nkey length=".strlen($key)."\n";
49   var_dump(bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode, $iv)));
50}
51
52$key = b'123456789012345678901234';
53echo "\n--- testing different iv lengths\n";
54foreach ($ivs as $iv) {
55   echo "\niv length=".strlen($iv)."\n";
56   var_dump(bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode, $iv)));
57}
58
59?>
60===DONE===
61--EXPECTF--
62*** Testing mcrypt_encrypt() : TripleDES functionality ***
63
64--- testing different key lengths
65
66key length=8
67
68Warning: mcrypt_encrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
69string(0) ""
70
71key length=20
72
73Warning: mcrypt_encrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
74string(0) ""
75
76key length=24
77string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac"
78
79key length=26
80
81Warning: mcrypt_encrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
82string(0) ""
83
84--- testing different iv lengths
85
86iv length=4
87
88Warning: mcrypt_encrypt(): Received initialization vector of size 4, but size 8 is required for this encryption mode in %s on line %d
89string(0) ""
90
91iv length=8
92string(112) "b85e21072239d60c63a80e7c9ae493cb741a1cd407e52f451c5f43a0d103f55a7b62617eb2e44213c2d44462d388bc0b8f119384b12c84ac"
93
94iv length=9
95
96Warning: mcrypt_encrypt(): Received initialization vector of size 9, but size 8 is required for this encryption mode in %s on line %d
97string(0) ""
98===DONE===
99