Home  >  Article  >  Backend Development  >  Code example of DES encryption and decryption in PHP

Code example of DES encryption and decryption in PHP

不言
不言forward
2019-04-01 09:53:552678browse

This article brings you code examples about DES encryption and decryption in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

test.php test file

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

Des.php

<?php

require_once(&#39;TripleDES.php&#39;);

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(&#39;mcrypt_decrypt&#39;)){
            $plain_text = mcrypt_decrypt(MCRYPT_TRIPLEDES, $key, $cipher_text, MCRYPT_MODE_CBC, $iv);
        }else{
            $plain_text = openssl_decrypt($cipher_text, &#39;DES-EDE3-CBC&#39;,$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);
    }

}

[Recommended course: PHP video tutorial

The above is the detailed content of Code example of DES encryption and decryption in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete