PHP字符串openssl_encrypt加密openssl_decrypt解密

2021-07-29 10:43:42 浏览数 (1)

参数说明:

$key密钥,固定8个字符,不能多也不能少

代码语言:php复制
<?php

//加密
function encryption($str,$key = 'gV0iX9aD') {
	$iv = $key;
	$data = openssl_encrypt($str,"DES-CBC",$key,OPENSSL_RAW_DATA,$iv);
	$data = strtolower(bin2hex($data));
	return $data;
} 

//解密 
function decryption($str,$key = 'gV0iX9aD') {	  
	$iv = $key;
	return openssl_decrypt (hex2bin($str), 'DES-CBC', $key, OPENSSL_RAW_DATA,$iv);
}

$data = "123456";

$encryption = encryption($data); //加密字符串123456

$decryption = decryption($encryption); //解密

echo $encryption; //输出加密结果(df6dc44aa296d763)

echo $decryption; //输出解密结果
key

0 人点赞