0

0

最近用PHP写的一个DES加密算法

PHP中文网

PHP中文网

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

|

1212人浏览过

|

来源于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

Magic AI Avatars
Magic AI Avatars

神奇的AI头像,获得200多个由AI制作的自定义头像。

下载
_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

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

32

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

23

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

16

2026.01.31

golang 循环遍历
golang 循环遍历

本专题整合了golang循环遍历相关教程,阅读专题下面的文章了解更多详细内容。

5

2026.01.31

Golang人工智能合集
Golang人工智能合集

本专题整合了Golang人工智能相关内容,阅读专题下面的文章了解更多详细内容。

6

2026.01.31

2026赚钱平台入口大全
2026赚钱平台入口大全

2026年最新赚钱平台入口汇总,涵盖任务众包、内容创作、电商运营、技能变现等多类正规渠道,助你轻松开启副业增收之路。阅读专题下面的文章了解更多详细内容。

268

2026.01.31

高干文在线阅读网站大全
高干文在线阅读网站大全

汇集热门1v1高干文免费阅读资源,涵盖都市言情、京味大院、军旅高干等经典题材,情节紧凑、人物鲜明。阅读专题下面的文章了解更多详细内容。

195

2026.01.31

无需付费的漫画app大全
无需付费的漫画app大全

想找真正免费又无套路的漫画App?本合集精选多款永久免费、资源丰富、无广告干扰的优质漫画应用,涵盖国漫、日漫、韩漫及经典老番,满足各类阅读需求。阅读专题下面的文章了解更多详细内容。

170

2026.01.31

漫画免费在线观看地址大全
漫画免费在线观看地址大全

想找免费又资源丰富的漫画网站?本合集精选2025-2026年热门平台,涵盖国漫、日漫、韩漫等多类型作品,支持高清流畅阅读与离线缓存。阅读专题下面的文章了解更多详细内容。

85

2026.01.31

热门下载

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

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
PHP课程
PHP课程

共137课时 | 10.7万人学习

JavaScript ES5基础线上课程教学
JavaScript ES5基础线上课程教学

共6课时 | 11.2万人学习

PHP新手语法线上课程教学
PHP新手语法线上课程教学

共13课时 | 0.9万人学习

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

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