加密原理:采用不同的加密算法對字符串進行加鹽加密處理。
用以防止密文被md5字典進行反向暴力破解。采用美國家安全局公布的加密算法(RFC 4357)加密,不采用自己創建的加密算法,以避免有安全漏洞。
以下是基于html' target='_blank'>Yii框架的實現代碼。
<?php/** * 密碼加密算法 * 對不同類型密碼采用不同的加密算法進行加密處理 * @author yagas<[email protected]> * @url http://blog.csdn.net/yagas * @version 0.1 * @example: * $passwd = new TPassword( TPassword::UserPassword ); * $passwd->encode( '123456' ); * $passwd->ckechPassword( 'xxxxxx', '123456' ); */class TPassword extends CModel { /** * 密碼鹽長度 * @var int */ private $_satlsLen = 5; /** * 鹽在密文中的偏移值 * @var int */ private $_offset = 10; /** * 加密算法名稱 * @var string */ private $_passwordType; /** * 會員登陸密碼 * @var string */ const UserPassword = 'sha224'; /** * 登陸員登陸密碼 * @var string */ const AdminPassword = 'snefru256'; /** * 支付密碼 * @var string */ const PayPassword = 'haval128,3'; public function __construct( $passwordType ) { $this->_passwordType = $passwordType; } public function attributeNames() { return array(); } /** * 加密字符串 * @param string $password 需要進行加密的字符串 * @param string $satls 加密鹽 * @return string 密文 */ public function encode( $password, $satls=null ) { if( is_null( $satls ) ) { $satls = ''; while( strlen( $satls ) > $this->_satlsLen ) { $i = mt_rand( 65, 90 ); $satls .= chr( $i ); } } $password = hash( $this->_passwordType, $password.$satls ); $password = md5( $password ); $newPassword = substr( $password, 0, $this->_offset ); $newPassword .= strtolower( $satls ) . substr( $password, $this->_offset ); return substr( $newPassword, 0, 32 ); } /** * 驗證密碼是否正確 * @param string $securtyString 密鑰 * @param string $password 密碼 * @return boolean */ public function checkPassword( $securtyString, $password ) { $satls = substr( $securtyString, $this->_offset, $this->_satlsLen ); $password = $this->encode( $password, strtoupper( $satls ) ); return $securtyString == $password; }}
PHP編程 鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。
新聞熱點
疑難解答