作者:asgvbsd | 来源:互联网 | 2018-03-23 03:01
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 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 DesKey->getSubKeyAt($round, $method); //子密钥
$FResult32Bit = $this->_F($subKey48Bit, $R); //轮函数结果
//异或
for ($index = 0; $index = 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 permutationETable); $index++) {
$tmp48Bit[] = $R[$this->permutationETable[$index] - 1];
}
//与子密钥异或
for ($index = 0; $index 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 permutationPTable); $index++) {
$FResult32Bit[] = $tem32Bit[$this->permutationPTable[$index] - 1];
}
return $FResult32Bit;
}
}
4. DesKey.class.php
_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 permutationTable); $index++) {
$tem48BitSubkey[] = $tem56BitCDkey[$this->permutationTable[$index] - 1];
}
$this->subKeyArr[] = $tem48BitSubkey;
if ($round <15) {
$this->_generateSubKey(++$round);
}
}
}
5. toolFunction.php
= 128) {
$boolArr[] = true;
} else {
$boolArr[] = false;
}
}
}
return $boolArr;
}
/**
* 测试用
* @param type $bitArr
*/
function dumpBit($bitArr) {
for ($index = 0; $index &#39;;
}
/**
* 测试用
* @param type $num
* @param type $str
*/
function testDesTime($num = 100, $str = &#39;测试测试&#39;) {
$startPoint = microtimeFloat();
for ($index = 0; $index <$num; $index++) {
$Des = new Des(&#39;12345678&#39;);
$encode = $Des->encode($str);
}
$endPoint = microtimeFloat();
$allTime = $endPoint - $startPoint;
return $allTime;
}
/**
* 测试用
* @param type $num
* @param type $str
*/
function testMd5Time($num = 100, $str = &#39;测试测试&#39;) {
$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);
}