class="java" name="code">
<?php
/**
+------------------------------------------------------------------------------
* DES加解密类
* 示例:
* Des::encrypt('content','key')
* Des::decrypt('encrypt','key')
+------------------------------------------------------------------------
*/
class Des
{
//DES加密
static function encrypt($encrypt,$key="")
{
$iv = mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND );
$passcrypt = mcrypt_encrypt ( MCRYPT_RIJNDAEL_256, $key, $encrypt, MCRYPT_MODE_ECB, $iv );
$encode = base64_encode ( $passcrypt );
return $encode;
}
//DES解密
static function decrypt($decrypt,$key="")
{
$decoded = base64_decode ( $decrypt );
$iv = mcrypt_create_iv ( mcrypt_get_iv_size ( MCRYPT_RIJNDAEL_256, MCRYPT_MODE_ECB ), MCRYPT_RAND );
$decrypted = mcrypt_decrypt ( MCRYPT_RIJNDAEL_256, $key, $decoded, MCRYPT_MODE_ECB, $iv );
return $decrypted;
}
}
?>
使用方法:
<?php
include("Des.class.php");
$key = "abc";
$data = array("username"=>"mochong", "weight"=>140);
$result = Des::encrypt(json_encode($data), $key);
print_r($result);
print_r("/n<br>");
$oriData = Des::decrypt($result, $key);
print_r($oriData);
?>