mcrypt_encrypt() Size of key is too large for this algorithm in …

function encrypt($str, $key) {

$block = mcrypt_get_block_size(‘des’, ‘ecb’);

$pad = $block – (strlen($str) % $block);

$str .= str_repeat(chr($pad), $pad);

$tt = mcrypt_encrypt(MCRYPT_DES,$key, $str,MCRYPT_MODE_ECB);

return base64_encode($tt);

}

function decrypt($str, $key) {

$str = base64_decode($str);

$str = mcrypt_decrypt(MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB); 

$block = mcrypt_get_block_size(‘des’, ‘ecb’);

$pad = ord($str[($len = strlen($str)) – 1]);

return substr($str, 0, strlen($str) – $pad);

}
在使用以上DES加密解密过程中报了错,经查原来是PHP版本中$key的长度不能超过8位,之前的KEY随机生成了14位。