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 up to 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'123456789012345678901234';
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
59
60Warning: mcrypt_encrypt(): Key of size 8 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
61string(0) ""
62
63key length=20
64
65Warning: mcrypt_encrypt(): Key of size 20 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
66string(0) ""
67
68key length=24
69string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27"
70
71key length=26
72
73Warning: mcrypt_encrypt(): Key of size 26 not supported by this algorithm. Only keys of size 24 supported in %s on line %d
74string(0) ""
75
76--- testing different iv lengths
77
78iv length=4
79string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27"
80
81iv length=8
82string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27"
83
84iv length=9
85string(112) "923eedcb20e18e3efa466a6ca1b842b34e6ac46aa3690ef739d0d68a26eb64e1a6ad42e7d18312ae8a57ab927e1dc892e5ff56c061864f27"
86===DONE===
87