0

0

纯php实现DES以及TripleDES加密算法

PHP中文网

PHP中文网

发布时间:2016-05-25 17:06:09

|

1804人浏览过

|

来源于php中文网

原创

1.example/php

encode($str);
echo "
Des加密结果:
"; echo $encode; $decode = $Des->decode($encode); echo "
Des解密结果:
"; echo $decode; $TripleDes = new TripleDes('12345678','abcdefgh'); $encode = $TripleDes->encode($str); echo "
3Des加密结果:
"; echo $encode; $decode = $TripleDes->decode($encode); echo "
3Des解密结果:
"; echo $decode; ?>

2.tripledes.class.php

DesArr[] = new Des($key1);
        $this->DesArr[] = new Des($key2);
    }
 
    public function encode($content) {
        return $this->DesArr[0]->encode(
                        $this->DesArr[1]->decode(
                                $this->DesArr[0]->encode($content)
                        )
        );
    }
     
    public function decode($content) {
        return $this->DesArr[0]->decode(
                        $this->DesArr[1]->encode(
                                $this->DesArr[0]->decode($content)
                        )
        );
    }
 
}
 
?>

3.des.class.php

DesKey = new DesKey($key);
    }
 
    public function encode($content) {
        return $this->authCode($content , 'encode' );
    }
 
    public function decode($content) {
        return $this->authCode($content , 'decode');
    }
 
    /**
     * 加密的启动函数
     * @param string $type 加密类型
     * @param type $content 加密内容
     * @return type 加密结果
     */
    public function authCode( $content , $type = 'encode') {
        if ($type != 'encode') {
            $type = 'decode';
        }
 
        $contentEncodeArr = array();
        $contentArr = str_split($content, 8);
 
        $encodeContent = '';
 
        for ($index = 0; $index < count($contentArr); $index++) {
            $content = $contentArr[$index];
            if (strlen($content) < 8) {
                $content .= str_repeat($this->contentAdd, ( 8 - strlen($content)));
 
 
            }
            $contentBitArr = bytesToBitArr($content);
            list($L, $R) = array_chunk($contentBitArr, 32);
 
            $contentEncodeArr = $this->_run($L, $R, $type);
 
            $byteArr = array_chunk($contentEncodeArr, 8);
 
            for ($index1 = 0; $index1 < count($byteArr); $index1++) {
                $byte = 0;
                for ($i = 0; $i < count($byteArr[$index1]); $i++) {
                    $byte += $byteArr[$index1][$i] * pow(2, 7 - $i);
                }
                $encodeContent .= chr($byte);
            }
        }
 
        return $encodeContent;
    }
 
    /**
     * Feistel 结构加密算法中的迭代函数
     * @param type $L 32位的左半部分输入
     * @param type $R 32位的右半部分输入
     * @param type $method encode(加密)或decode(解密) 
     * @param type $round 迭代的轮数
     * @return type
     */
    private function _run($L, $R, $method = "encode", $round = 0) {
        $nextL = ''; //下轮左半部分输入
        $nextR = ''; //下轮右半部分输入
 
        $subKey48Bit = $this->DesKey->getSubKeyAt($round, $method); //子密钥
        $FResult32Bit = $this->_F($subKey48Bit, $R); //轮函数结果
        //异或
        for ($index = 0; $index < count($FResult32Bit); $index++) {
            $FResult32Bit[$index] = $FResult32Bit[$index] === $L[$index] ? false : true;
        }
 
        $nextL = $R;
        $nextR = $FResult32Bit;
 
        //轮数将会停在15,共加密16轮
        if ($round >= 15) {
            return array_merge($nextR, $nextL);
        } else {
            return $this->_run($nextL, $nextR, $method, ++$round);
        }
    }
 
    /**
     * Feitel架构中的轮函数
     * @param type $subKey48Bit 48位的子密钥
     * @param type $R 当前轮的右部分输入
     * @return 32位结果
     */
    public function _F($subKey48Bit, $R) {
        $tmp48Bit = array();
 
        //E表置换
        for ($index = 0; $index < count($this->permutationETable); $index++) {
            $tmp48Bit[] = $R[$this->permutationETable[$index] - 1];
        }
 
        //与子密钥异或
        for ($index = 0; $index < count($tmp48Bit); $index++) {
            $tmp48Bit[$index] = $tmp48Bit[$index] === $subKey48Bit[$index] ? false : true;
        }
 
        //代替/选择(s盒)
        $tem32Bit = array();
        $tem6BitArr = array_chunk($tmp48Bit, 6);
        for ($index = 0; $index < count($tem6BitArr); $index++) {
            $tem6Bit = $tem6BitArr[$index];
 
            $line = $tem6Bit[0] * 2 + $tem6Bit[5] * 1;
            $field = $tem6Bit[1] * 2 * 2 * 2 + $tem6Bit[2] * 2 * 2 + $tem6Bit[3] * 2 + $tem6Bit[4];
            $selectPos = $line * 6 + $field;
 
            $select = $this->sBox[$index][$selectPos];
            if ($select >= 8) {
                $tem32Bit[] = true;
            } else {
                $tem32Bit[] = false;
            }
            $select %= 8;
            if ($select >= 4) {
                $tem32Bit[] = true;
            } else {
                $tem32Bit[] = false;
            }
            $select %= 4;
            if ($select >= 2) {
                $tem32Bit[] = true;
            } else {
                $tem32Bit[] = false;
            }
            $select %=2;
            if ($select >= 1) {
                $tem32Bit[] = true;
            } else {
                $tem32Bit[] = false;
            }
        }
 
        //置换P
        $FResult32Bit = array();
        for ($index = 0; $index < count($this->permutationPTable); $index++) {
            $FResult32Bit[] = $tem32Bit[$this->permutationPTable[$index] - 1];
        }
 
        return $FResult32Bit;
    }
 
}

4.deskey.class.php

白月生产企业订单管理系统GBK2.0  Build 080807
白月生产企业订单管理系统GBK2.0 Build 080807

请注意以下说明:1、本程序允许任何人免费使用。2、本程序采用PHP+MYSQL架构编写。并且经过ZEND加密,所以运行环境需要有ZEND引擎支持。3、需要售后服务的,请与本作者联系,联系方式见下方。4、本程序还可以与您的网站想整合,可以实现用户在线服务功能,可以让客户管理自己的信息,可以查询自己的订单状况。以及返点信息等相关客户利益的信息。这个功能可提高客户的向心度。安装方法:1、解压本系统,放在

下载
_initKey($key);
        $this->_generateSubKey();
    }
    /**
     * 获取指定位置的子密钥
     * @param type $index 位置
     * @param type $method 决定是正序还是逆序
     * @return type 48bit的子密钥
     */
    public function getSubKeyAt($index, $method = 'encode') {
        if ($method == 'encode') {
            return $this->subKeyArr[$index];
        } else {
            return $this->subKeyArr[15 - $index];
        }
    }
 
    /**
     * 初始化64位的密钥
     * @param type $key 字符密钥
     */
    private function _initKey($key) {
        $key = substr($key, 0, 8);
        $keyLen = strlen($key);
        //补全64位
        if ($keyLen < 8) {
            $key .= str_repeat($this->keyAdd, 8 - $keyLen);
        }
 
        $this->key = $key;
 
        $bitArr = bytesToBitArr($this->key);
 
        //初始化C0,左边取28位
        for ($index = 0; $index < 32; $index++) {
            if ($index % 8 === 7) {
                continue;
            }
            $this->CkeyArr[] = $bitArr[$index];
        }
         
        //初始化D0,右边取28位
        for ($index = 32; $index < 64; $index++) {
            if ($index % 8 === 7) {
                continue;
            }
            $this->DkeyArr[] = $bitArr[$index];
        }
    }
     
    /**
     * 16轮生成16个子密钥
     * @param type $round 当前轮数
     */
    private function _generateSubKey($round = 0) {
        //左移
        $tmp28BitC = array_shift($this->CkeyArr);
        $tmp28BitD = array_shift($this->DkeyArr);
        $this->CkeyArr[] = $tmp28BitC;
        $this->DkeyArr[] = $tmp28BitD;
        //是否继续左移
        if ($this->leftShiftArr[$round] == 2) {
            $tmp28BitC = array_shift($this->CkeyArr);
            $tmp28BitD = array_shift($this->DkeyArr);
            $this->CkeyArr[] = $tmp28BitC;
            $this->DkeyArr[] = $tmp28BitD;
        }
         
        $tem56BitCDkey = array_merge($this->CkeyArr, $this->DkeyArr);
        $tem48BitSubkey = array();
        //置换&压缩
        for ($index = 0; $index < count($this->permutationTable); $index++) {
            $tem48BitSubkey[] = $tem56BitCDkey[$this->permutationTable[$index] - 1];
        }
        $this->subKeyArr[] = $tem48BitSubkey;
         
        if ($round < 15) {
            $this->_generateSubKey(++$round);
        }
    }
 
}

5.toolfunction.php

立即学习PHP免费学习笔记(深入)”;

= 128) {
                    $boolArr[] = true;
                } else {
                    $boolArr[] = false;
                }
            }
        }
 
        return $boolArr;
    }
 
    /**
     * 测试用
     * @param type $bitArr
     */
    function dumpBit($bitArr) {
        for ($index = 0; $index < count($bitArr); $index++) {
            if ($bitArr[$index]) {
                echo '1';
            } else {
                echo '0';
            }
        }
 
        echo '
'; } /** * 测试用 * @param type $num * @param type $str */ function testDesTime($num = 100, $str = '测试测试') { $startPoint = microtimeFloat(); for ($index = 0; $index < $num; $index++) { $Des = new Des('12345678'); $encode = $Des->encode($str); } $endPoint = microtimeFloat(); $allTime = $endPoint - $startPoint; return $allTime; } /** * 测试用 * @param type $num * @param type $str */ function testMd5Time($num = 100, $str = '测试测试') { $startPoint = microtimeFloat(); for ($index = 0; $index < $num; $index++) { $encode = md5($str); } $endPoint = microtimeFloat(); $allTime = $endPoint - $startPoint; return $allTime; } /** * 测试用 * @param type $num * @param type $str */ function microtimeFloat() { list($usec, $sec) = explode(" ", microtime()); return ((float) $usec + (float) $sec); }
PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

相关专题

更多
mc.js网页版入口地址大全
mc.js网页版入口地址大全

本专题整合了mc.js网页版入口地址大全以及mc.js1.8.8版本汇总,阅读专题下面的文章了解更多详细内容。

0

2026.01.05

Python lambda详解
Python lambda详解

本专题整合了Python lambda函数相关教程,阅读下面的文章了解更多详细内容。

0

2026.01.05

python处理大数据合集
python处理大数据合集

本专题整合了python处理大数据相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.05

大数据专业学习教程
大数据专业学习教程

本专题整合了大数据专业学习相关教程,阅读专题下面的文章了解更多详细内容。

0

2026.01.05

python设置中文版教程合集
python设置中文版教程合集

本专题整合了python改成中文版相关教程,阅读专题下面的文章了解更多详细内容。

1

2026.01.05

从零到实战:Python 编程系统入门专题
从零到实战:Python 编程系统入门专题

本专题面向零编程基础及初学者,系统讲解 Python 编程语言的核心知识与实战技巧。内容涵盖 Python 基础语法、数据结构、函数与模块、常用标准库、简单算法思维,以及真实应用场景下的小项目实战。通过循序渐进的学习路径,帮助读者快速建立编程思维,掌握 Python 在数据处理、自动化脚本及日常开发中的实际应用能力,为后续深入学习 Web 开发、数据分析或人工智能打下坚实基础。

10

2026.01.05

php代码编辑器入口汇总
php代码编辑器入口汇总

本文整理了主流PHP代码编辑器的官网入口及在线使用链接,阅读专题下面的文章了解更多详细内容。

51

2026.01.04

php代码编辑器地址汇总
php代码编辑器地址汇总

本文整理了主流PHP代码编辑器的官网入口及在线使用链接,阅读专题下面的文章了解更多详细内容。

0

2026.01.04

Excel制作交互图表的方法 Excel交互式的图表教程大全
Excel制作交互图表的方法 Excel交互式的图表教程大全

Excel交互式图表可通过四种方法实现:一、用切片器控制数据透视图;二、结合下拉列表与INDEX-MATCH动态引用;三、用选项按钮绑定图表系列;四、利用动态命名区域配合OFFSET函数。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

330

2026.01.04

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号