网站建设资讯

NEWS

网站建设资讯

PHP中DES加解密的案例-创新互联

小编给大家分享一下PHP中DES加解密的案例,希望大家阅读完这篇文章后大所收获,下面让我们一起去探讨吧!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、雅安服务器托管、营销软件、网站建设、郊区网站维护、网站推广。

test.php测试文件

'AbcdefghijklmnopqrstuvwX','secretcode'=>'Abcdefgh'];
$encode = $des->encode($data, $conf);
print_r($encode);
echo "
"; $decode = $des->decode($encode,$conf); print_r($decode); ?>

Des.php

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

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

}

看完了这篇文章,相信你对PHP中DES加解密的案例有了一定的了解,想了解更多相关知识,欢迎关注创新互联行业资讯频道,感谢各位的阅读!


当前文章:PHP中DES加解密的案例-创新互联
网页网址:http://cdweb.net/article/ddijog.html