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$cipher = MCRYPT_TRIPLEDES;
20$mode = MCRYPT_MODE_ECB;
21$data = b'This is the secret message which must be encrypted';
22
23// tripledes uses keys upto 192 bits (24 bytes)
24$keys = array(
25   b'12345678',
26   b'12345678901234567890',
27   b'123456789012345678901234',
28   b'12345678901234567890123456'
29);
30
31echo "\n--- testing different key lengths\n";
32foreach ($keys as $key) {
33   echo "\nkey length=".strlen($key)."\n";
34   var_dump(bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode)));
35}
36
37$key = b'12345678';
38$ivs = array(
39   b'1234',
40   b'12345678',
41   b'123456789'
42);
43
44// ivs should be ignored in ecb mode
45echo "\n--- testing different iv lengths\n";
46foreach ($ivs as $iv) {
47   echo "\niv length=".strlen($iv)."\n";
48   var_dump(bin2hex(mcrypt_encrypt($cipher, $key, $data, $mode, $iv)));
49}
50
51?>
52===DONE===
53--EXPECTF--
54*** Testing mcrypt_encrypt() : TripleDES functionality ***
55
56--- testing different key lengths
57
58key length=8
59string(112) "05c9c4cafb9937d950bdae60ee3abcb8d9f3e1f1fac8acaaa5b11d70b7ca02f3b76d447ab3dd85a4b5df5dedb4b4654595ccdf6da97fa93f"
60
61key length=20
62string(112) "0fc7045c4fb4dbcf44baf9ed15ab40331a42ff0632318a16b12ed5873f02e7945e4f63f408d6849534cbb7419c22c8854aaa85e0e05a28e6"
63
64key length=24
65string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27"
66
67key length=26
68
69Warning: mcrypt_encrypt(): Size of key is too large for this algorithm in %s on line %d
70string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27"
71
72--- testing different iv lengths
73
74iv length=4
75string(112) "05c9c4cafb9937d950bdae60ee3abcb8d9f3e1f1fac8acaaa5b11d70b7ca02f3b76d447ab3dd85a4b5df5dedb4b4654595ccdf6da97fa93f"
76
77iv length=8
78string(112) "05c9c4cafb9937d950bdae60ee3abcb8d9f3e1f1fac8acaaa5b11d70b7ca02f3b76d447ab3dd85a4b5df5dedb4b4654595ccdf6da97fa93f"
79
80iv length=9
81string(112) "05c9c4cafb9937d950bdae60ee3abcb8d9f3e1f1fac8acaaa5b11d70b7ca02f3b76d447ab3dd85a4b5df5dedb4b4654595ccdf6da97fa93f"
82===DONE===
83