程式碼備份錄——PHP7.2 加解密之—— DES-CBC
加解密
public static function encrypt($str,$key){
$str = urlencode($str);
$str = base64_encode($str);
$str = self::pkcsPadding($str, 8);
// 此處根據自己不同偏移量進行修改
$v = $key;//pack('H*', "1234567890abcdef");
$key = str_pad($key, 8, '0'); // 3DES加密將8改為24
$sign = openssl_encrypt($str, 'DES-CBC', $key,OPENSSL_RAW_DATA | OPENSSL_NO_PADDING,$v);
$sign = strtoupper(bin2hex($sign));
return $sign;
}
//des解密(cbc模式)
public static function decrypt($encrypted,$key){
$encrypted = hex2bin($encrypted);
// 此處根據自己不同偏移量進行修改
$v = $key;//pack('H*', "1234567890abcdef");
$key = str_pad($key, 8, '0'); // 3DES加密將8改為24
$sign = @openssl_decrypt($encrypted, 'DES-CBC', $key,OPENSSL_RAW_DATA | OPENSSL_NO_PADDING,$v);
$sign = self::unPkcsPadding($sign);
$sign = rtrim($sign);
$sign = base64_decode($sign);
$sign = urldecode($sign);
return $sign;
}
本作品採用《CC 協議》,轉載必須註明作者和本文連結