字串加密解密演算法_PHP教程

來源:互聯網
上載者:User
php5.5中有更為可靠和方便的加密方式。喜歡鑽研的朋友可以瞭解一下:

password_hash()

http://www.php.net/manual/zh/function.password-hash.php

基於mcrypt擴充,按位異或總結的兩個字串加密解密演算法

php/** * @info 字串加密解密演算法一,利用mcrypt擴充 * @param string $string 待處理字串 *        $action ENCODE,加密 | DECODE,解密 * @return string $returnstr * @date 2014/4/22 * @author tonglei */function mcrypt_handle_string($string, $action = 'ENCODE'){        !is_array($string) or exit;        $action == 'DECODE' && $string = base64_decode($string);        $key = "123456"; //key可自訂或在設定檔中擷取        $mcryptAlgorithm = MCRYPT_DES; //選擇一種密碼編譯演算法        $mcryptMode = MCRYPT_MODE_ECB; //選擇一種加密模式        $mcryptIv = mcrypt_create_iv(mcrypt_get_iv_size($mcryptAlgorithm, $mcryptMode),                MCRYPT_RAND); //建立初始化向量        $returnstr = base64_encode(mcrypt_encrypt($mcryptAlgorithm, $key, $string, $mcryptMode,                $mcryptIv));        if('DECODE' == $action)        {                $returnstr = mcrypt_decrypt($mcryptAlgorithm, $key, $string, $mcryptMode, $mcryptIv);        }        return $returnstr;}

php/** * * @info 字串加密解密演算法二 利用按位異或 * @param string $string 待處理字串 * @param $action ENCODE 加密 | DECODE 解密 * @return string */function StrCode($string, $action = 'ENCODE'){        $action != 'ENCODE' && $string = base64_decode($string);        $code = '';        $key = substr(md5($GLOBALS['pwServer']['HTTP_USER_AGENT'] . $GLOBALS['db_hash']),                8, 18);        $keyLen = strlen($key);        $strLen = strlen($string);        for ($i = 0; $i < $strLen; $i++)        {                $k = $i % $keyLen;                $code .= $string[$i] ^ $key[$k];        }        return ($action != 'DECODE' ? base64_encode($code) : $code);}

http://www.bkjia.com/PHPjc/769761.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/769761.htmlTechArticlephp5.5中有更為可靠和方便的加密方式。喜歡鑽研的朋友可以瞭解一下: password_hash() http://www.php.net/manual/zh/function.password-hash.php 基於mcrypt擴充...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.