最新ucenter cookie加密

写道
function authcode($string, $operation = 'DECODE', $key = '', $expiry = 0) {

$ckey_length = 4; // 随机密钥长度 取值 0-32;
// 加入随机密钥,可以令密文无任何规律,即便是原文和密钥完全相同,加密结果也会每次不同,增大破解难度。
// 取值越大,密文变动规律越大,密文变化 = 16 的 $ckey_length 次方
// 当此值为 0 时,则不产生随机密钥

$key = md5($key ? $key : UC_KEY);
$keya = md5(substr($key, 0, 16));
$keyb = md5(substr($key, 16, 16));
$keyc = $ckey_length ? ($operation == 'DECODE' ? substr($string, 0, $ckey_length): substr(md5(microtime()), -$ckey_length)) : '';

$cryptkey = $keya.md5($keya.$keyc);
$key_length = strlen($cryptkey);

$string = $operation == 'DECODE' ? base64_decode(substr($string, $ckey_length)) : sprintf('%010d', $expiry ? $expiry + time() : 0).substr(md5($string.$keyb), 0, 16).$string;
$string_length = strlen($string);

$result = '';
$box = range(0, 255);

$rndkey = array();
for($i = 0; $i <= 255; $i++) {
$rndkey[$i] = ord($cryptkey[$i % $key_length]);
}

for($j = $i = 0; $i < 256; $i++) {
$j = ($j + $box[$i] + $rndkey[$i]) % 256;
$tmp = $box[$i];
$box[$i] = $box[$j];
$box[$j] = $tmp;
}

for($a = $j = $i = 0; $i < $string_length; $i++) {
$a = ($a + 1) % 256;
$j = ($j + $box[$a]) % 256;
$tmp = $box[$a];
$box[$a] = $box[$j];
$box[$j] = $tmp;
$result .= chr(ord($string[$i]) ^ ($box[($box[$a] + $box[$j]) % 256]));
}

if($operation == 'DECODE') {
if((substr($result, 0, 10) == 0 || substr($result, 0, 10) - time() > 0) && substr($result, 10, 16) == substr(md5(substr($result, 26).$keyb), 0, 16)) {
return substr($result, 26);
} else {
return '';
}
} else {
return $keyc.str_replace('=', '', base64_encode($result));
}

}


自己用java整合ucenter,发现网上的都不能用。看看下php的authcode,照着写了下,发现怎么写都会有个bug,请高手搞一下。
---接口
package com.hq.encryptor;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import com.hq.encryptor.iface.PasswordEnDe;

/**
 * Ucenter论坛可逆加密算法
 * bug:解密后的字符串可能多一个或者2个字符。谁解决了请发我信箱supttkl@163.com
 * @author GAO
 *
 */
public class UcenterPasswordEncryptor implements PasswordEnDe {
    // 密钥字符串
    private String ENCRYPTOR_KEY = "1234567890";
    private BASE64Decoder decoder = new BASE64Decoder();
    private BASE64Encoder encoder = new BASE64Encoder();

    public UcenterPasswordEncryptor() {

    }

    /**
     * @param key 密钥
     */
    public UcenterPasswordEncryptor(String key) {
        this.ENCRYPTOR_KEY = key;
    }

    // 加密
    public String encrypt(String password) {
        try {
            password=URLEncoder.encode(password, "UTF-8");//支持中文
            String key = ENCRYPTOR_KEY;
            int ckey_length = 4;
            key = getMd5String(key);
            String keya = getMd5String(key.substring(0, 16));
            String keyb = getMd5String(key.substring(16, 32));
            String min = String.valueOf(System.currentTimeMillis());
            min = "0.00000000 " + min.substring(0, min.length() - 3);
            String keyc = ckey_length > 0 ? getMd5String(String.valueOf(min))
                    : "";
            keyc = keyc.substring(keyc.length() - 4, keyc.length());
            String cryptkey = keya + getMd5String(keya + keyc);
            int key_length = cryptkey.length();
            byte[] temp = null;
            String str_temp = "0000000000"
                    + getMd5String(password + keyb).substring(0, 16) + password;
            temp = str_temp.getBytes("UTF-8");
            int[] box = new int[256];
            for (int i = 0; i < box.length; i++) {
                box[i] = i;
            }

            char[] rndkey = new char[256];
            for (int i = 0; i <= 255; i++) {
                rndkey[i] = cryptkey.charAt(i % key_length);
            }

            for (int j = 0, i = 0; i < 256; i++) {
                j = (j + box[i] + (int) rndkey[i]) % 256;
                int tmp = box[i];
                box[i] = box[j];
                box[j] = tmp;
            }
            for (int a = 0, j = 0, i = 0; i < temp.length; i++) {
                a = (a + 1) % 256;
                j = (j + box[a]) % 256;
                int tmp = box[a];
                box[a] = box[j];
                box[j] = tmp;
                int gao = (int) temp[i];
                byte c = (byte) (gao ^ (box[(box[a] + box[j]) % 256]));
                temp[i] = c;
            }
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
            ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(
                    temp);
            encoder.encode(((InputStream) (bytearrayinputstream)),
                    ((OutputStream) (bytearrayoutputstream)));
            String s = bytearrayoutputstream.toString();
            return keyc + s.replace("=", "");
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private String getMd5String(String password)
            throws NoSuchAlgorithmException {
        MessageDigest md = MessageDigest.getInstance("MD5");
        byte[] input = md.digest(password.getBytes());
        String hex = byteToHexString(input);
        return hex;
    }

    private String byteToHexString(byte[] res) {
        StringBuffer sb = new StringBuffer(res.length << 1);
        for (int i = 0; i < res.length; i++) {
            String digit = Integer.toHexString(0xFF & res[i]);
            if (digit.length() == 1) {
                digit = '0' + digit;
            }
            sb.append(digit);
        }
        return sb.toString();
    }
    //
    public boolean matches(String passwordToCheck, String storedPassword) {
          if(storedPassword == null) {
                throw new NullPointerException("storedPassword can not be null");
            }
            if(passwordToCheck == null) {
                throw new NullPointerException("passwordToCheck can not be null");
            }
           
            return encrypt(passwordToCheck).equals(storedPassword);
    }

    // 解密
    public String decrypt(String password) {
        try {
           
            int ckey_length = 4;
            String key = getMd5String(ENCRYPTOR_KEY);
            String keya = getMd5String(key.substring(0, 16));
            String min = String.valueOf(System.currentTimeMillis());
            min = "0.00000000 " + min.substring(0, min.length() - 3);
           
            String keyc = ckey_length > 0 ? password.substring(0, ckey_length)
                    : "";
            String cryptkey = keya + getMd5String(keya + keyc);
            int key_length = cryptkey.length();
            byte[] temp = null;
            if(true){//这步为出bug的地方
                String t1 = password.substring(ckey_length, password.length());
                ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(t1.getBytes("UTF-8"));
                ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
                decoder.decodeBuffer(((InputStream) (bytearrayinputstream)), ((OutputStream) (bytearrayoutputstream)));
                temp=bytearrayoutputstream.toByteArray();
            }
           
            int[] box=new int[256];
            for (int i = 0; i <box.length; i++) {
                box[i] = i;
            }
           
            char[] rndkey = new char[256];
            for (int i = 0; i <= 255; i++) {
                rndkey[i] = cryptkey.charAt(i % key_length);
            }

            for (int j = 0, i = 0; i < 256; i++) {
                j = (j + box[i] + (int) rndkey[i]) % 256;
                int tmp = box[i];
                box[i] = box[j];
                box[j] = tmp;
            }
            StringBuffer sb = new StringBuffer();
            for(int a =0, j=0, i = 0; i < temp.length; i++) {
                a = (a + 1) % 256;
                j = (j + box[a]) % 256;
                int tmp = box[a];
                box[a] = box[j];
                box[j] = tmp;
                int gao=(int)temp[i]<0? (int)temp[i]+256:(int)temp[i];
                char c =(char)(gao ^ (box[(box[a] + box[j]) % 256]));
                sb.append(c);
            } 
            String lastStr= sb.substring(26, sb.length());
            return URLDecoder.decode(lastStr, "UTF-8");
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

}


Discuz!登陆验证Cookie机制分析

在构建我的vita系统的过程中,发现管理员管理的便捷与系统安全隐患之间的矛盾
全站采用cookie验证,比如wordpress的验证就是基于cookie的,由于cookie的明文传输
在局域网内极易被截获,或者这个vita在我不发骚的情况下存在了XSS漏洞的话,cookie被人截获,
在这种情况下,等于站点被人xxx了

另一种情况就是利用session来进行管理员身份的认证,但是由于php天生对于session的处理机制的问题,不能长时间保存,利用数据库构建的session系统开销太大,在这种情况下,我就只好先研究先下大家是怎么做的

于是分析了Discuz!的登陆验证机制

每个Discuz!论坛都有一个特定的authkey也就是Discuz!程序中的$_DCACHE['settings']['authkey']并且与用户的浏览器特征值HTTP_USER_AGENT一起组成了discuz_auth_key这个变量如下代码:
commone.inc.php文件大概130行左右

$discuz_auth_key = md5($_DCACHE['settings']['authkey'].$_SERVER['HTTP_USER_AGENT']);


在Discuz!论坛用户登陆以后会有一个cookie,名称为cdb_auth(cdb_是你站点的名称,可以设置不能在config.inc.php 文件中设置),Discuz!论坛就靠这个来判断一个用户是否是登陆状态,在分析这个值的内容之前,我们看下他是如何生成的

list($discuz_pw, $discuz_secques, $discuz_uid) = empty($_DCOOKIE['auth']) ? array('', '', 0) : daddslashes(explode("\t", authcode($_DCOOKIE['auth'], 'DECODE')), 1);

解释一下,获得的客户端的cookie经过Discuz!的函数authcode解密以后会得到用户输入的用户名,密码,在authcode函数中 会用到刚刚提到的$discuz_auth_key这个值,在不知道$discuz_auth_key的情况下,基本上靠cookie里的值反解出用户名 密码的几率为0,同样的,在生成cdb_auth就是相逆的一个流程,先获得用户输入的用户名,密码,在验证正确之后,用authcode加密,写入 cookie,很简单吧

以上就是Discuz!普通用户的登陆验证过程,写的不是很详细,大概能看明白就行

 

 

站一直以来用firefox浏览ajax的功能都有问题,报错是:“XML解析错误:xml处理指令不在实体的开始部分 ”。

这是由于discuz返回的xml在最开始的地方有一个空行,IE解释没问题,但是firefox把空行作为一个节点,造成解释出错。

找了很久都不知道哪里来的空行,经过排查发现是include头文件产生的:

require_once './include/common.inc.php';

 

我也很难知道是common.inc.php文件哪里产生的空行,因此我加了两行代码屏蔽了这个文件的输出:

ob_start();
require_once './include/common.inc.php';
ob_end_clean();

 

问题解决。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值