PHP-libmcrypt-2.4.x加密-AES加密

來源:互聯網
上載者:User

標籤:libmcrypt   php的aes   ecb   mcrypt_rijndael_128   encrypt和decrypt   

mcrypt_get_block_size — 獲得密碼編譯演算法的分組大小

此函數將在PHP7.1.0以後將被棄用。


int mcrypt_get_block_size ( int $cipher )
int mcrypt_get_block_size ( string $cipher , string $mode )

第一個原型針對 libmcrypt 2.2.x, 第二個原型針對 libmcrypt 2.4.x 或 2.5.x。


PHP的AES加、解密類:

<?phpclass Security {public static function encrypt($input, $key) {// define (‘MCRYPT_RIJNDAEL_128‘, "rijndael-128");// 16位元組,128位// define (‘MCRYPT_MODE_ECB‘, "ecb");// 常用加密模式$size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);// 填充空位;pkcs5_pad&pkcs7_pad二者區別不大$input = Security::pkcs5_pad($input, $size);$td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, ‘‘, MCRYPT_MODE_ECB, ‘‘);$iv = mcrypt_create_iv (mcrypt_enc_get_iv_size($td), MCRYPT_RAND);mcrypt_generic_init($td, $key, $iv);$data = mcrypt_generic($td, $input);mcrypt_generic_deinit($td);mcrypt_module_close($td);$data = base64_encode($data);return $data;}                 /**         * $text 加密的字串         * $blocksize 區塊大小,16位元組;值為16         */private static function pkcs5_pad ($text, $blocksize) {$pad = $blocksize - (strlen($text) % $blocksize);// 使用$pad的ascii碼值來填充空位return $text . str_repeat(chr($pad), $pad);} public static function decrypt($sStr, $sKey) {$decrypted= mcrypt_decrypt(MCRYPT_RIJNDAEL_128,$sKey,base64_decode($sStr),MCRYPT_MODE_ECB);$dec_s = strlen($decrypted);$padding = ord($decrypted[$dec_s-1]);$decrypted = substr($decrypted, 0, -$padding);return $decrypted;}}   // $key = "1234567891234567";// $data = ‘{"user":"1234556789","pwd":"4343434343","tel":"18988888888","realname":"zhangsan"}‘; // $value = Security::encrypt($data , $key );// echo "加密::".$value.‘<br/>‘;// echo Security::decrypt($value, $key );


代碼中使用:

   /**     * 加密、解密     */    function zm_ecrypt()    {        // 加密、解密        $string = ‘{"user":"1234556789","pwd":"4343434343","tel":"18988888888","realname":"zhangsan"}‘;                vendor(‘encrypt.Security‘) or die("方案7引入失敗");        $sec = new \Security();        // 這裡對第二個參數key進行了base64_encode加密        $sec_res1 = $sec->encrypt($string, "em1hcnRlAzIwMTc5ODc2NTQzMjEwMTIz");        // 結果轉16進位        $sec_res = toHexString(base64_decode($sec_res1));                // 解密        // 先把十六進位轉成字串,然後進行base64_encode        $demo = base64_encode(hexToStr($sec_res));        $sec_dec = $sec->decrypt($demo, "em1hcnRlAzIwMTc5ODc2NTQzMjEwMTIz");//         var_dump("\r\n方案7加密的結果\r\n" . $sec_res);//         var_dump("\r\n方案7解密的結果\r\n" . $sec_dec);//         return $as_res_encrypt;    }

使用的時候,請注意base64_encode和base64_decode,對key和string是否進行編碼。


用到的十六進位與字串之間的轉換:

   /**     * 十六進位轉字串     * 16進位的轉為2進位字串         * @param 十六進位 $hex     * @return string     */    function hexToStr($hex)    {        $string="";        for($i=0;$i<strlen($hex)-1;$i+=2)            $string.=chr(hexdec($hex[$i].$hex[$i+1]));        return  $string;    }        /**     * 將$string轉換成十六進位     * @param string $string     * @return stream     */    function toHexString ($string){        $buf = "";        for ($i = 0; $i < strlen($string); $i++){            $val = dechex(ord($string{$i}));            if(strlen($val)< 2)                $val = "0".$val;            $buf .= $val;        }        return $buf;    }


本文出自 “為了以後” 部落格,謝絕轉載!

PHP-libmcrypt-2.4.x加密-AES加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.