1--TEST-- 2openssl_decrypt() tests dependent on openssl_encrypt 3--EXTENSIONS-- 4openssl 5--FILE-- 6<?php 7$data = "openssl_encrypt() and openssl_decrypt() tests"; 8$method = "AES-128-CBC"; 9$password = "openssl"; 10 11$ivlen = openssl_cipher_iv_length($method); 12$iv = ''; 13srand(time() + ((int)(microtime(true) * 1000000) % 1000000)); 14while(strlen($iv) < $ivlen) $iv .= chr(rand(0,255)); 15 16$encrypted = openssl_encrypt($data, $method, $password, 0, $iv); 17$output = openssl_decrypt($encrypted, $method, $password, 0, $iv); 18var_dump($output); 19$encrypted = openssl_encrypt($data, $method, $password, OPENSSL_RAW_DATA, $iv); 20$output = openssl_decrypt($encrypted, $method, $password, OPENSSL_RAW_DATA, $iv); 21var_dump($output); 22// if we want to manage our own padding 23$padded_data = $data . str_repeat(' ', 16 - (strlen($data) % 16)); 24$encrypted = openssl_encrypt($padded_data, $method, $password, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv); 25$output = openssl_decrypt($encrypted, $method, $password, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv); 26var_dump(rtrim($output)); 27 28$output2 = openssl_decrypt($encrypted, $method, $password, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv, tag: ''); 29var_dump($output2 === $output); 30$output3 = openssl_decrypt($encrypted, $method, $password, OPENSSL_RAW_DATA|OPENSSL_ZERO_PADDING, $iv, tag: null); 31var_dump($output3 === $output); 32 33if (in_array("bf-ecb", openssl_get_cipher_methods())) { 34 // if we want to prefer variable length cipher setting 35 $encrypted = openssl_encrypt($data, "bf-ecb", $password, OPENSSL_DONT_ZERO_PAD_KEY); 36 $output = openssl_decrypt($encrypted, "bf-ecb", $password, OPENSSL_DONT_ZERO_PAD_KEY); 37 var_dump($output === $data); 38} else { 39 var_dump(true); 40} 41 42// It's okay to pass $tag for a non-authenticated cipher. 43// It will be populated with null in that case. 44openssl_encrypt($data, $method, $password, 0, $iv, $tag); 45var_dump($tag); 46 47?> 48--EXPECT-- 49string(45) "openssl_encrypt() and openssl_decrypt() tests" 50string(45) "openssl_encrypt() and openssl_decrypt() tests" 51string(45) "openssl_encrypt() and openssl_decrypt() tests" 52bool(true) 53bool(true) 54bool(true) 55NULL 56