PHP字符串加密

PHP字符串加密

需要开启Crypt扩展



$data = '需要要加密的字符串';

// 密钥 16位
$key = 'SADFo92jzVnzSj39';

$iv = substr( hash('SHA256', mt_rand()), 13, 32 );

// 加密
$enc = mcrypt_encrypt( MCRYPT_RIJNDAEL_256, $key, $data, MCRYPT_MODE_CBC, $iv );

// 解密
$dec = mcrypt_decrypt( MCRYPT_RIJNDAEL_256, $key, $enc, MCRYPT_MODE_CBC, $iv );

使用 openssl 扩展


$str = 'Hello World!++--';

$key = 'myKey0123456789+';

$iv = substr(md5(time()), 4, 16); // len 16

$encode = openssl_encrypt($str, 'aes-256-cfb', $key, 0, $iv);

$decode = openssl_decrypt($encode, 'aes-256-cfb', $key, 0, $iv);

var_dump($encode);