溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP中DES加解密的案例

發(fā)布時(shí)間:2020-10-29 11:30:47 來源:億速云 閱讀:135 作者:小新 欄目:編程語(yǔ)言

小編給大家分享一下PHP中DES加解密的案例,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

test.php測(cè)試文件

<?php
require_once('Des.php');
$des = new Des();
$data['a'] = 'a';
$data['b'] = 'b';
$conf = ['appkey'=>'AbcdefghijklmnopqrstuvwX','secretcode'=>'Abcdefgh'];
$encode = $des->encode($data, $conf);
print_r($encode);
echo "<br>";
$decode = $des->decode($encode,$conf);
print_r($decode);
?>

Des.php

<?php

require_once('TripleDES.php');

class Des {

    public static function encode($data, $configKey) {
        $tripleDes = new TripleDES();
        if (is_array($data)) {
            $data = json_encode($data);
        }
        return $tripleDes->encode($data, $configKey["appkey"], $configKey["secretcode"]);
    }

    public static function decode($data, $configKey) {
        $tripleDes = new TripleDES();
        return $tripleDes->decode($data, $configKey["appkey"], $configKey["secretcode"]);
    }

    public static function encodeArr($data, $configKey) {
        $data = json_encode($data);
        return self::encode($data, $configKey);
    }

    public static function decodeArr($data, $configKey) {
        $res = self::decode($data, $configKey);
        return json_decode($res,true);
    }

}

TripleDES.php

<?php

class TripleDES {

    public static function genIvParameter() {
        return mcrypt_create_iv(mcrypt_get_iv_size(MCRYPT_TRIPLEDES, MCRYPT_MODE_CBC), MCRYPT_RAND);
    }

    private static function pkcs5Pad($text, $blocksize) {
        $pad = $blocksize - (strlen($text) % $blocksize); // in php, strlen returns the bytes of $text
        return $text . str_repeat(chr($pad), $pad);
    }

    private static 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);
    }

    public static function encryptText($plain_text, $key, $iv) {
        $padded = TripleDES::pkcs5Pad($plain_text, mcrypt_get_block_size(MCRYPT_TRIPLEDES, MCRYPT_MODE_CBC));
        return mcrypt_encrypt(MCRYPT_TRIPLEDES, $key, $padded, MCRYPT_MODE_CBC, $iv);
    }

    public static function decryptText($cipher_text, $key, $iv) {
        if(function_exists('mcrypt_decrypt')){
            $plain_text = mcrypt_decrypt(MCRYPT_TRIPLEDES, $key, $cipher_text, MCRYPT_MODE_CBC, $iv);
        }else{
            $plain_text = openssl_decrypt($cipher_text, 'DES-EDE3-CBC',$key, OPENSSL_NO_PADDING,$iv);
        }
        return TripleDES::pkcs5Unpad($plain_text);
    }

    public static function decode($cipher_text, $key, $iv) {
        $cipher_text = base64_decode($cipher_text);
        $cipher_text = TripleDES::decryptText($cipher_text, $key, $iv);
        return $cipher_text;
    }

    public static function encode($cipher_text, $key, $iv) {
        $cipher_text = TripleDES::encryptText($cipher_text, $key, $iv);
        return base64_encode($cipher_text);
    }

}

看完了這篇文章,相信你對(duì)PHP中DES加解密的案例有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI