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