DES加密算法,是一种对称加密算法,加解密都使用同一个密钥。
OpenSSL扩展,是PHP常用的密码扩展之一。OpenSSL扩展封装了很多加密算法,而且不需要写很多复杂的代码,很多加密算法可以直接调用其函数实现。但是,在使用OpenSSL扩展的过程中,需要注意很多细节。如果不了解其函数的使用方法,具体传入参数的要求,可能要走很多弯路。
最近,笔者用OpenSSL实现了DES的两种加密模式的加解密。
/**
* DES 解密函数
*
* @param string $ciphertext 密文
* @param string $method 加密学方式('DES-ECB')
* @param string $password 密钥
*/
function desecb_decrypt($ciphertext, $password, $method = 'DES-ECB', $options = OPENSSL_ZERO_PADDING)
{
$hex = hex2bin($ciphertext);
$data = base64_encode($hex);
$plaintext = openssl_decrypt($data, $method, $password, $options);
return trim($plaintext);
}
/**
* DES 加密函数
*
* @param string $plaintext 明文
* @param string $method 解密方式(