兼容PHP和Java的DES加解密

虽然DES已经不安全,有时候还是会用到

/**
 *
 * @param unknown $str
 * @param unknown $key
 * @return string
 */
function desEncrypt($str, $key) {
	$block = mcrypt_get_block_size ( 'des', 'ecb' );
	$pad = $block - (strlen ( $str ) % $block);
	$str .= str_repeat ( chr ( $pad ), $pad );
	return mcrypt_encrypt ( MCRYPT_DES, $key, $str, MCRYPT_MODE_ECB );
}

/**
 *
 * @param unknown $str
 * @param unknown $key
 * @return string
 */
function desDecrypt($str, $key) {
	$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 );
}

 

发表评论

电子邮件地址不会被公开。

*