Skip to content

Commit

Permalink
全部使用unix换行符
Browse files Browse the repository at this point in the history
  • Loading branch information
cmpan committed Aug 8, 2017
1 parent ca70203 commit 8409eca
Show file tree
Hide file tree
Showing 6 changed files with 504 additions and 504 deletions.
78 changes: 39 additions & 39 deletions lib/CryptInterface.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,40 @@
<?php
/**
* Windwork
*
* 一个用于快速开发高并发Web应用的轻量级PHP框架
*
* @copyright Copyright (c) 2008-2017 Windwork Team. (http://www.windwork.org)
* @license http://opensource.org/licenses/MIT
*/
namespace wf\crypt;

/**
* 可逆加密接口
*
* @package wf.crypt
* @author cm <[email protected]>
* @link http://docs.windwork.org/manual/wf.crypt.html
* @since 0.1.0
*/
interface CryptInterface
{
/**
* 加密,原字串经过私有密匙加密
*
* @param string 等待加密的原字串
* @param string 私有密匙(用于解密和加密)
* @return string
*/
public function encrypt($txt, $key);

/**
* 解密,字串经过私有密匙解密
*
* @param string 加密后的字串
* @param string 私有密匙(用于解密和加密)
* @return string
*/
public function decrypt($txt, $key);

<?php
/**
* Windwork
*
* 一个用于快速开发高并发Web应用的轻量级PHP框架
*
* @copyright Copyright (c) 2008-2017 Windwork Team. (http://www.windwork.org)
* @license http://opensource.org/licenses/MIT
*/
namespace wf\crypt;

/**
* 可逆加密接口
*
* @package wf.crypt
* @author cm <[email protected]>
* @link http://docs.windwork.org/manual/wf.crypt.html
* @since 0.1.0
*/
interface CryptInterface
{
/**
* 加密,原字串经过私有密匙加密
*
* @param string 等待加密的原字串
* @param string 私有密匙(用于解密和加密)
* @return string
*/
public function encrypt($txt, $key);

/**
* 解密,字串经过私有密匙解密
*
* @param string 加密后的字串
* @param string 私有密匙(用于解密和加密)
* @return string
*/
public function decrypt($txt, $key);

}
46 changes: 23 additions & 23 deletions lib/Exception.php
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
<?php
/**
* Windwork
*
* 一个用于快速开发高并发Web应用的轻量级PHP框架
*
* @copyright Copyright (c) 2008-2017 Windwork Team. (http://www.windwork.org)
* @license http://opensource.org/licenses/MIT
*/
namespace wf\crypt;

/**
* 加密异常类
* @package wf.crypt
* @author cm <[email protected]>
* @since 0.1.0
*/
class Exception extends \Exception
{

}

<?php
/**
* Windwork
*
* 一个用于快速开发高并发Web应用的轻量级PHP框架
*
* @copyright Copyright (c) 2008-2017 Windwork Team. (http://www.windwork.org)
* @license http://opensource.org/licenses/MIT
*/
namespace wf\crypt;

/**
* 加密异常类
* @package wf.crypt
* @author cm <[email protected]>
* @since 0.1.0
*/
class Exception extends \Exception
{

}


226 changes: 113 additions & 113 deletions lib/strategy/AzDG.php
Original file line number Diff line number Diff line change
@@ -1,113 +1,113 @@
<?php
/**
* Windwork
*
* 一个用于快速开发高并发Web应用的轻量级PHP框架
*
* @copyright Copyright (c) 2008-2017 Windwork Team. (http://www.windwork.org)
* @license http://opensource.org/licenses/MIT
*/
namespace wf\crypt\strategy;

/**
* 可逆对称加密解密
* 极高效的对称加密算法
* 参考Discuz! Passport采用的Azerbaijan Development Group(AzDG)开发的可逆加密算法
*
* @package wf.crypt.strategy
* @author cm <[email protected]>
* @link http://docs.windwork.org/manual/wf.crypt.html
* @since 0.1.0
*/
class AzDG implements \wf\crypt\CryptInterface
{
/**
* 加密
*
* @param string 等待加密的原字串
* @param string 私有密匙(用于解密和加密)
*
* @return string 原字串经过私有密匙加密后的结果
*/
public function encrypt($txt, $key)
{
// 使用随机数发生器产生 0~32000 的值并 MD5()
srand((double)microtime() * 1000000);
$encryptKey = md5(rand(0, 32000));

// 变量初始化
$ctr = 0;
$tmp = '';

// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
for($i = 0; $i < strlen($txt); $i++) {
// 如果 $ctr = $encryptKey 的长度,则 $ctr 清零
$ctr = $ctr == strlen($encryptKey) ? 0 : $ctr;
// $tmp 字串在末尾增加两位,其第一位内容为 $encryptKey 的第 $ctr 位,
// 第二位内容为 $txt 的第 $i 位与 $encryptKey 的 $ctr 位取异或。然后 $ctr = $ctr + 1
$tmp .= $encryptKey[$ctr].($txt[$i] ^ $encryptKey[$ctr++]);
}

// 返回结果,结果为 self::key() 函数返回值的 base64 编码结果
return base64_encode(self::key($tmp, $key));
}

/**
* 解密
*
* @param string 加密后的字串
* @param string 私有密匙(用于解密和加密)
*
* @return string 字串经过私有密匙解密后的结果
*/
public function decrypt($txt, $key)
{
// $txt 的结果为加密后的字串经过 base64 解码,然后与私有密匙一起,
// 经过 self::key() 函数处理后的返回值
$txt = self::key(base64_decode($txt), $key);

// 变量初始化
$tmp = '';

// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
for ($i = 0; $i < strlen($txt); $i++) {
// $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
// 与 $txt 的第 $i + 1 位取异或。然后 $i = $i + 1
$tmp .= $txt[$i] ^ $txt[++$i];
}

// 返回 $tmp 的值作为结果
return $tmp;
}

/**
* 密匙处理
*
* @param string 待加密或待解密的字串
* @param string 私有密匙(用于解密和加密)
*
* @return string 处理后的密匙
*/
private static function key($txt, $encryptKey)
{
// 将 $encryptKey 赋为 $encryptKey 经 md5() 后的值
$encryptKey = md5($encryptKey);

// 变量初始化
$ctr = 0;
$tmp = '';

// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
for($i = 0; $i < strlen($txt); $i++) {
// 如果 $ctr = $encryptKey 的长度,则 $ctr 清零
$ctr = $ctr == strlen($encryptKey) ? 0 : $ctr;
// $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
// 与 $encryptKey 的第 $ctr + 1 位取异或。然后 $ctr = $ctr + 1
$tmp .= $txt[$i] ^ $encryptKey[$ctr++];
}

// 返回 $tmp 的值作为结果
return $tmp;
}
}
<?php
/**
* Windwork
*
* 一个用于快速开发高并发Web应用的轻量级PHP框架
*
* @copyright Copyright (c) 2008-2017 Windwork Team. (http://www.windwork.org)
* @license http://opensource.org/licenses/MIT
*/
namespace wf\crypt\strategy;

/**
* 可逆对称加密解密
* 极高效的对称加密算法
* 参考Discuz! Passport采用的Azerbaijan Development Group(AzDG)开发的可逆加密算法
*
* @package wf.crypt.strategy
* @author cm <[email protected]>
* @link http://docs.windwork.org/manual/wf.crypt.html
* @since 0.1.0
*/
class AzDG implements \wf\crypt\CryptInterface
{
/**
* 加密
*
* @param string 等待加密的原字串
* @param string 私有密匙(用于解密和加密)
*
* @return string 原字串经过私有密匙加密后的结果
*/
public function encrypt($txt, $key)
{
// 使用随机数发生器产生 0~32000 的值并 MD5()
srand((double)microtime() * 1000000);
$encryptKey = md5(rand(0, 32000));

// 变量初始化
$ctr = 0;
$tmp = '';

// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
for($i = 0; $i < strlen($txt); $i++) {
// 如果 $ctr = $encryptKey 的长度,则 $ctr 清零
$ctr = $ctr == strlen($encryptKey) ? 0 : $ctr;
// $tmp 字串在末尾增加两位,其第一位内容为 $encryptKey 的第 $ctr 位,
// 第二位内容为 $txt 的第 $i 位与 $encryptKey 的 $ctr 位取异或。然后 $ctr = $ctr + 1
$tmp .= $encryptKey[$ctr].($txt[$i] ^ $encryptKey[$ctr++]);
}

// 返回结果,结果为 self::key() 函数返回值的 base64 编码结果
return base64_encode(self::key($tmp, $key));
}

/**
* 解密
*
* @param string 加密后的字串
* @param string 私有密匙(用于解密和加密)
*
* @return string 字串经过私有密匙解密后的结果
*/
public function decrypt($txt, $key)
{
// $txt 的结果为加密后的字串经过 base64 解码,然后与私有密匙一起,
// 经过 self::key() 函数处理后的返回值
$txt = self::key(base64_decode($txt), $key);

// 变量初始化
$tmp = '';

// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
for ($i = 0; $i < strlen($txt); $i++) {
// $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
// 与 $txt 的第 $i + 1 位取异或。然后 $i = $i + 1
$tmp .= $txt[$i] ^ $txt[++$i];
}

// 返回 $tmp 的值作为结果
return $tmp;
}

/**
* 密匙处理
*
* @param string 待加密或待解密的字串
* @param string 私有密匙(用于解密和加密)
*
* @return string 处理后的密匙
*/
private static function key($txt, $encryptKey)
{
// 将 $encryptKey 赋为 $encryptKey 经 md5() 后的值
$encryptKey = md5($encryptKey);

// 变量初始化
$ctr = 0;
$tmp = '';

// for 循环,$i 为从 0 开始,到小于 $txt 字串长度的整数
for($i = 0; $i < strlen($txt); $i++) {
// 如果 $ctr = $encryptKey 的长度,则 $ctr 清零
$ctr = $ctr == strlen($encryptKey) ? 0 : $ctr;
// $tmp 字串在末尾增加一位,其内容为 $txt 的第 $i 位,
// 与 $encryptKey 的第 $ctr + 1 位取异或。然后 $ctr = $ctr + 1
$tmp .= $txt[$i] ^ $encryptKey[$ctr++];
}

// 返回 $tmp 的值作为结果
return $tmp;
}
}

Loading

0 comments on commit 8409eca

Please sign in to comment.