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
请注意以下说明: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);
}










