新聞中心
如何在php中使用加密解密DES類?相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
php的框架有哪些
php的框架:1、Laravel,Laravel是一款免費(fèi)并且開源的PHP應(yīng)用框架。2、Phalcon,Phalcon是運(yùn)行速度最快的一個(gè)PHP框架。3、Symfony,Symfony是一款為Web項(xiàng)目準(zhǔn)備的PHP框架。4、Yii,Yii是一款快速、安全和專業(yè)的PHP框架。5、CodeIgniter,CodeIgniter是一款非常敏捷的開源PHP框架。6、CakePHP,CakePHP是一款老牌的PHP框架。7.Kohana,Kohana是一款敏捷但是功能強(qiáng)大的PHP框架。
代碼1:
class DES { var $key; // 密鑰 var $iv; // 偏移量 function __construct( $key, $iv=0 ) { $this->key = $key; if( $iv == 0 ) { $this->iv = $key; } else { $this->iv = $iv; // 創(chuàng)建初始向量, 并且檢測(cè)密鑰長(zhǎng)度, Windows 平臺(tái)請(qǐng)使用 MCRYPT_RAND // mcrypt_create_iv ( mcrypt_get_block_size (MCRYPT_DES, MCRYPT_MODE_CBC), MCRYPT_DEV_RANDOM ); } } function encrypt($str) { //加密,返回大寫十六進(jìn)制字符串 $size = mcrypt_get_block_size ( MCRYPT_DES, MCRYPT_MODE_CBC ); $str = $this->pkcs5Pad ( $str, $size ); // bin2hex 把 ASCII 字符的字符串轉(zhuǎn)換為十六進(jìn)制值 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 ); } }
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; DES5 has a deprecated constructor in D:\phpstudy_pro\WWW\des\DES5.php on line 2
Fatal error: Uncaught Error: Call to undefined function mcrypt_get_block_size() in D:\phpstudy_pro\WWW\des\DES5.php:19 Stack trace: #0 D:\phpstudy_pro\WWW\des\1.php(10): DES5->encrypt('podsmia') #1 {main} thrown in D:\phpstudy_pro\WWW\des\DES5.php on line 19
mcrypt_cbc 以 CBC 模式加解密數(shù)據(jù), 在PHP 5.5.0+被棄用, PHP 7.0.0被移除
mcrypt_encrypt / mcrypt_decrypt 使用給定參數(shù)加密 / 解密, 在PHP 7.1.0+被棄用, 在PHP 7.2.0+被移除
代碼2:
class DES7 { //要改的加密, 使用 openssl public function desEncrypt($str,$key) { $iv = $key; $data = openssl_encrypt($str,"DES-CBC",$key,OPENSSL_RAW_DATA,$iv); $data = strtolower(bin2hex($data)); return $data; } //要改的解密 public function desDecrypt($str,$key) { $iv = $key; return openssl_decrypt (hex2bin($str), 'DES-CBC', $key, OPENSSL_RAW_DATA,$iv); } }
看完上述內(nèi)容,你們掌握如何在php中使用加密解密DES類的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
分享題目:如何在php中使用加密解密DES類-創(chuàng)新互聯(lián)
分享鏈接:http://www.dlmjj.cn/article/ddphjh.html