Home > Backend Development > PHP Tutorial > PHP版本DES加密解(对应.net版与JAVA版)

PHP版本DES加密解(对应.net版与JAVA版)

WBOY
Release: 2016-06-23 13:37:38
Original
904 people have browsed it

/** *  * DES FOR .NET版本 * @author Administrator * */class DES_NET{    var $key;    var $iv; //偏移量         function DES_NET( $key, $iv=0 ) {    //key长度8例如:1234abcd        $this->key = $key;        if( $iv == 0 ) {            $this->iv = $key; //默认以$key 作为 iv        } else {            $this->iv = $iv; //mcrypt_create_iv ( mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM );        }    }         function encrypt($str) {    //加密,返回大写十六进制字符串        $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) {    //解密        $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版本 * @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);	}	}
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template