class DES
{
var $key;
var $iv; //偏移量
function __construct($key)
{
//key长度为8位,例如:1234abcd
$this->key = $key;
$this->iv = $key; //默认以$key 作为 iv
}
//加密
function encrypt($str)
{
$size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC);
$str = $this->pkcs5Pad($str, $size);
return strtoupper(bin2hex(mcrypt_encrypt(MCRYPT_DES, $this->key, $str, MCRYPT_MODE_CBC, $this->iv)));
}
//解密
function decrypt($str)
{
$strBin = $this->hex2bin(strtolower($str));
$str = mcrypt_decrypt(MCRYPT_DES, $this->key, $strBin, MCRYPT_MODE_CBC, $this->iv);
$str = $this->pkcs5Unpad($str);
return $str;
}
function hex2bin($hexData)
{
$binData = "";
for ($i = 0; $i < strlen($hexData); $i += 2) {
$binData .= chr(hexdec(substr($hexData, $i, 2)));
}
return $binData;
}
function pkcs5Pad($text, $blocksize)
{
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
function pkcs5Unpad($text)
{
$pad = ord($text{strlen($text) - 1});
if ($pad > strlen($text))
return false;
if (strspn($text, chr($pad), strlen($text) - $pad) != $pad)
return false;
return substr($text, 0, -1 * $pad);
}
}
加密字符串:{"Version":"1.0.0.0","Token":"IW0CxVq1bABsy0tPjgKnCA==","RequestId":"5dc116a26128d","RequestTime":"2020-04-01T16:14:50","Message":{"OrderNoList":["DS191201093408476673"]}}
加密秘钥key:V9hUUEkL
加密后字符串:D9CC1E97735B1F86B6C158C6D3796D206A26D740C6F462494E3FCE0284D3F1DA22460008CC7179AFBA98404104AA89180CA6546A50DEC34FEC77F29B2640C988D0EA9E7A3DD35D88308F9BDF4289D6BE5B27E470FEC97337F7136E92E756EE10ADCC7E396517AE38A4B8ADE47CDE77E905FF76349715F443BAE383E91091DF492799CDBD7C2E2B583663DDB5A8860D6F5D494EC2651455BD190CEDDCD116B862BEC9E6123742A048CAAFEAA0BE1CDA49
解密后字符串:{"Version":"1.0.0.0","Token":"IW0CxVq1bABsy0tPjgKnCA==","RequestId":"5dc116a26128d","RequestTime":"2020-04-01T16:14:50","Message":{"OrderNoList":["DS191201093408476673"]}}