PHP中DES加解密的代码示例
发布:smiling 来源: PHP粉丝网 添加日期:2020-02-11 20:14:24 浏览: 评论:0
本篇文章给大家带来的内容是关于PHP中DES加解密的代码示例,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
test.php测试文件
- <?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>";
- //phpfensi.com
- $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;
- }
- //phpfensi.com
- public static function encode($cipher_text, $key, $iv) {
- $cipher_text = TripleDES::encryptText($cipher_text, $key, $iv);
- return base64_encode($cipher_text);
- }
- }
Tags: DES加解密
相关文章
- ·PHP DES加解密方法代码(2020-02-22)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)