/** * * DES for. NET version * @author Administrator * */class des_net{var $key; var $iv; Offset function des_net ($key, $iv =0) {//key length 8 for example: 1234ABCD $this->key = $key; if ($iv = = 0) {$this->iv = $key;//default to $key as IV} else {$this->iv = $iv;//mcrypt_c Reate_iv (Mcrypt_get_block_size (Mcrypt_des, MCRYPT_MODE_CBC), mcrypt_dev_random); }} function Encrypt ($STR) {//encryption, return uppercase hexadecimal string $size = Mcrypt_get_block_size (Mcrypt_des, Mcrypt_mode _CBC); $str = $this->pkcs5pad ($str, $size); Return Strtoupper (Bin2Hex (MCRYPT_CBC (mcrypt_des, $this->key, $str, Mcrypt_encrypt, $this->iv))); } function Decrypt ($STR) {//Decrypt $strBin = $this->hex2bin (strtolower ($STR)); $str = MCRYPT_CBC (mcrypt_des, $this->key, $strBin, 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); }}/** * * DES JAVA version * @author Administrator * */class des_java{var $key; function Des_java ($key) {$this->key = $ Key;} function Encrypt ($encrypt) {$encrypt = $this->pkcs5_pad ($encrypt); $iv = Mcrypt_create_iv (Mcrypt_get_iv_size ( Mcrypt_des, MCRYPT_MODE_ECB), mcrypt_rand); $passcrypt = Mcrypt_encrypt (mcrypt_des, $this->Key, $encrypt, MCRYPT_MODE_ECB, $iv); return Strtoupper (Bin2Hex ($passcrypt));} function Decrypt ($decrypt) {//$decoded = Base64_decode ($decrypt), $decoded = Pack ("h*", $decrypt); $iv = Mcrypt_create_iv (Mcrypt_get_iv_size (Mcrypt_des, MCRYPT_MODE_ECB), mcrypt_rand); $decrypted = Mcrypt_decrypt (Mcrypt_des, $this Key, $decoded, MCRYPT_MODE_ECB, $iv), return $this->pkcs5_unpad ($decrypted);} function Pkcs5_unpad ($text) {$pad = Ord ($text {strlen ($text)-1}), if ($pad > strlen ($text)) return $text; if (STRSPN ($ Text, Chr ($pad), strlen ($text)-$pad)! = $pad) return $text; return substr ($text, 0,-1 * $pad);} function Pkcs5_pad ($text) {$len = strlen ($text); $mod = $len% 8; $pad = 8-$mod; return $text. Str_repeat (Chr ($pad), $pad);}}