<?phpclass des{ var $key; var $iv; //偏移量 function des( $key, $iv=0 ) { //key长度8例如:1234abcd $this->key = $key; if( $iv == 0 ) { $this->iv = $key; } 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 ); }}$my_encrypt = new des('qwertyuiop');echo $my_encrypt->encrypt('1234567');
试试这个: http://www.cnblogs.com/wangchuang/archive/2012/04/23/2466002.html
楼上说的这个的测试结果:
Notice: Uninitialized string offset: 6 in C:\web\project1\web\dec.php on line 148
Notice: Uninitialized string offset: 7 in C:\web\project1\web\dec.php on line 148
Notice: Uninitialized string offset: 6 in C:\web\project1\web\dec.php on line 148
Notice: Uninitialized string offset: 7 in C:\web\project1\web\dec.php on line 148
MDN2IAFG6iMABKVbI2l+KA==
hello levenPHP Notice: Uninitialized string offset: 6 in C:\web\project1\web\dec.php on line 148 PHP Notice: Uninitialized string offset: 7 in C:\web\project1\web\dec.php on line 148 PHP Notice: Uninitialized string offset: 6 in C:\web\project1\web\dec.php on line 148 PHP Notice: Uninitialized string offset: 7 in C:\web\project1\web\dec.php on line 148
function encrypt($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);}function decrypt($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); }
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号