来看代码了!
无聊的时候瞎写的,欢迎大虾们指正
Common\NDV.php
/**
* NDV (Not complete Digital Visa)
* 非完整数字签证
*
* +----------------------------------------------------+
* * NDV(基础函数库文件),属于KSEF(Kee Simply Efficient Framework)项目扩展库函数
* @author Kee <chinboy2012@gmail.com>
* @date 2012年12月10日
* @version Alpha 1.0
* +----------------------------------------------------+
*/
class NDV{
private $Str_list = array();
private $SafeStr = null;
private $BinCode = null;
private $Md5Code = null;
private $Sh1Code = null;
private $SafeCode = null;
public function NDVa($str, $mode = 'NDV'){
$this-> NDV_list($str);
$this-> NDV_Safe_Add();
$this-> NDV_4in1();
if($mode == 'NDV'){
//输出52位混合编码,默认选项
return $this-> SafeCode;
}elseif($mode == 'BIN'){
//输出20位二进制校验码
return $this-> BinCode;
}elseif($mode == 'MD5'){
//输出32为MD5校验码
return $this-> Md5Code;
}elseif($mode == 'SH1'){
//输出32位Sh1校验码
return $this-> Sh1Code;
}
}
private function NDV_list($str) {
$Str_Len = strlen($str); //计算字符串长度
$Str_Arr = array(); //拆解字符串数组
$Str_list = array(); //混序列表
// $mod3 = $Str_Len%3; //模3值
$Len3 = intval($Str_Len / 3); //除3取整
$list = 0;
//进行一次循环,拆解字符串,混序排列
for ($i = 0, $x = 0; $i < $Str_Len; $x++) {
$Str_Arr[$x] = md5(md5(substr($str, $i, 3)) . md5($str)); //拆解字符串,取字符串摘要获取64位混合编码
//混序排列
$this->BinCode .= $Str_Arr[$x] % 2;
if ($Str_Arr[$x] % 2 == 1) {
//如果模2值为1,则序列下沉
$Str_list[$Len3] = $Str_Arr[$x];
$Len3--;
} elseif ($Str_Arr[$x] % 2 == 0) {
//如果模2值为0,则序列上浮
$Str_list[$list] = $Str_Arr[$x];
$list++;
}
$i+=3;
}
$this->Str_list = $Str_list;
// return $this; //返回数据
}
private function NDV_Safe_Add() {
$safenum = null;
if(count($this-> Str_list) >= 16){
//如果混合序列大于或等于16组,则进行数据合并,提取512位安全编码
for($i=0; $i<16; $i++){
$this-> SafeStr .= (String)($this-> Str_list[$i]);
}
}else {
$safenum = 16-count($this-> Str_list);
for($i=count($this-> Str_list); $i<16; $i++){
//不足16组,验证数据补充
@$this-> Str_list[$i] = md5($this->BinCode.md5($this-> Str_list[$i-1]));
$this-> BinCode .= $this-> Str_list[$i]%2;
}
for($i=0; $i<16; $i++){
//提取512位混合编码
@$this-> SafeStr .= (String)($this-> Str_list[$i]);
}
}
$this-> Md5Code = md5($this-> SafeStr);
$this-> Sh1Code = substr(sha1($this-> SafeStr), 0, 32);
return $this;
}
private function NDV_4in1(){
//四选一
$se = array();
$se[0] = substr($this-> SafeStr, 0, 128);
$this-> BinCode .= $se[0]%2;
$se[1] = substr($this-> SafeStr, 128, 128);
$this-> BinCode .= $se[1]%2;
$se[2] = substr($this-> SafeStr, 256, 128);
$this-> BinCode .= $se[2]%2;
$se[3] = substr($this-> SafeStr, 384, 128);
$this-> BinCode .= $se[3]%2;
$senum = $this-> SafeStr%4;
$SafeCode = md5($se[$senum]);
if($SafeCode%2 == 0){
$SafeCode = substr($SafeCode, 0, 16);
}elseif($SafeCode%2 == 1){
$SafeCode = substr($SafeCode, 16, 16);
}
$SafeCode .= $this-> BinCode;
$this-> SafeCode = $SafeCode;
return $this;
}
}
function NDV($str , $mode = 'NDV'){
$ndv = new NDV();
return $ndv-> NDVa($str,$mode);
}
$string = '111111111111';
var_dump(NDV($string));
var_dump(NDV($string,'BIN'));
var_dump(NDV($string,'MD5'));
var_dump(NDV($string,'SH1'));
乱七八糟的还是挺多的,而且也挺不伦不类的,因为原本就没打算写类的,只是用类封装方便些!
欢迎大虾们指正!!这个是Alpha版的,还有很多问题...
主要的作用是对用户密码进行数字签证,因为是对数据进行混序填充,破坏原信息签证,添加二进制码进行重新签证,所以叫做不完整数字签证,整个过程有点混乱,大虾们多担待...嘿嘿!
[解决办法]
似乎一套程序里边用不到这么多的加密方式吧,基本上一个md5的function就能统领全局了.
不过捏,楼主的作为还是值得称赞的.
[解决办法]
一般来说,如果算法不公开,给一个加盐sha1已经能够应付绝大部分的攻击了。如果你的盐足够长,例如2048位,则基本不可能被碰撞。
当然,你这种打乱的算法,就算公开了也不容易被碰撞,安全性自然更高一筹。
[解决办法]
楼主的算法有理论依据吗?
我们知道 MD5 有碰撞,并且理论上很容易计算得到
那么为什么 MD5 会有碰撞呢?
道理很简单,当你试图将无限的宇宙放进一个有限的空间里时,碰撞就必然发生了
既然 MD5 有碰撞,那么无论对已碰撞的 MD5 数据做什么变形,他还是“碰撞的”