PHP实现采用CBC模式,PKCS5Padding填充,DES加密的数据加密方式

当PHP版本小于7.1

1
2
3
4
5
6
7
function mcrypt_des_cbc_pkcs5padding($text, $key, $iv)
{
$blockSize = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);
$pad = $blockSize - (strlen($text) % $blockSize);
$text = $text . str_repeat(chr($pad), $pad);
return base64_encode(mcrypt_encrypt(MCRYPT_DES, substr($key,0,8), $text, MCRYPT_MODE_CBC, $iv));
}

当PHP版本大于7.1

1
2
3
4
5
function mcrypt_des_cbc_pkcs5padding($text, $key, $iv)
{
$encrypted = openssl_encrypt($text, "DES-CBC", $key, OPENSSL_RAW_DATA, $iv);
return base64_encode($encrypted);
}