如何在PHP中利用DES進(jìn)行加密與解密-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)如何在PHP中利用DES進(jìn)行加密與解密,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

我們提供的服務(wù)有:成都做網(wǎng)站、成都網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、微信公眾號(hào)開(kāi)發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、岳池ssl等。為千余家企事業(yè)單位解決了網(wǎng)站和推廣的問(wèn)題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的岳池網(wǎng)站制作公司

php中有一個(gè)擴(kuò)展可以支持DES的加密算法,是:extension=php_mcrypt.dll


在配置文件中將這個(gè)擴(kuò)展打開(kāi)還不能夠在windows環(huán)境下使用

需要將PHP文件夾下的 libmcrypt.dll 拷貝到系統(tǒng)的 system32 目錄下,這是通過(guò)phpinfo可以查看到mcrypt表示這個(gè)模塊可以正常試用了。

下面是PHP中使用DES加密解密的一個(gè)例子:


復(fù)制代碼 代碼如下:


//$input - stuff to decrypt
    //$key - the secret key to use

    function do_mencrypt($input, $key)
    {
        $input = str_replace(""n", "", $input);
        $input = str_replace(""t", "", $input);
        $input = str_replace(""r", "", $input);
        $key = substr(md5($key), 0, 24);
        $td = mcrypt_module_open('tripledes', '', 'ecb', '');
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, $key, $iv);
        $encrypted_data = mcrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return trim(chop(base64_encode($encrypted_data)));
    }

    //$input - stuff to decrypt
    //$key - the secret key to use

    function do_mdecrypt($input, $key)
    {
        $input = str_replace(""n", "", $input);
        $input = str_replace(""t", "", $input);
        $input = str_replace(""r", "", $input);
        $input = trim(chop(base64_decode($input)));
        $td = mcrypt_module_open('tripledes', '', 'ecb', '');
        $key = substr(md5($key), 0, 24);
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, $key, $iv);
        $decrypted_data = mdecrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        return trim(chop($decrypted_data));

    }


關(guān)于如何在PHP中利用DES進(jìn)行加密與解密就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前標(biāo)題:如何在PHP中利用DES進(jìn)行加密與解密-創(chuàng)新互聯(lián)
標(biāo)題網(wǎng)址:http://bm7419.com/article46/didphg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、標(biāo)簽優(yōu)化、品牌網(wǎng)站制作外貿(mào)建站網(wǎng)頁(yè)設(shè)計(jì)公司、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

手機(jī)網(wǎng)站建設(shè)