这有一组测试数据:之前接触过c#加解密,现在首次接触php,总是出现各种问题,请各位帮个忙,看我下面的代码哪里出错了,运行时总是提示“Fatal error: Call to undefined function mcrypt_get_block_size()”错误,php.ini里该去的注释去了,也尝试把此文件拷贝到SYSTEM32下,都不行,该有的.dll文件都有,谁知道还有其他什么原因导致这种问题呢?
key:3535353535353535
data:1234567890ABCDEF
result:E3D1570D3604719D
<?php
/*
php 数据 加密解密 类文件
*/
class DES
{
var $key='3535353535353535';
var $iv; //偏移量
function DES($key, $iv=0)
{
$this->key = $key;
if($iv == 0)
{
$this->iv = $key;
}
else
{
$this->iv = $iv;
}
}
//加密
static function encrypt($str)
{
$size = mcrypt_get_block_size(MCRYPT_DES, MCRYPT_MODE_CBC );
$str = $this->pkcs5Pad ( $str, $size );
$data=mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_ENCRYPT, $this->iv);
//$data=strtoupper(bin2hex($data)); //返回大写十六进制字符串
return base64_encode($data);
}
//解密
function decrypt($str)
{
$str = base64_decode ($str);
//$strBin = $this->hex2bin( strtolower($str));
$str = mcrypt_cbc(MCRYPT_DES, $this->key, $str, MCRYPT_DECRYPT, $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 );
}
}
echo DES::encrypt('encrypt($str)');
?>