Jive 2.5 SkinUtils.java 中的 encodePasswordCookie decodePasswordCookie

package code;

public class T3 {
	private final static int  ENCODE_XORMASK = 0x5A; // 十六进制 90
    private final static char ENCODE_DELIMETER = '\002'; // \转义字符 stx
    private final static char ENCODE_CHAR_OFFSET1 = 'A';
    private final static char ENCODE_CHAR_OFFSET2 = 'h';
	public static void main(String[] args) {
		System.out.println(T3.encodePasswordCookie("chen", "chen"));
		System.out.println(T3.decodePasswordCookie("JkDkJkDkMmMkIhEhMh"));
	}
	
    private static String encodePasswordCookie(String username, String password)
    {
        StringBuffer buf = new StringBuffer();
        if (username != null && password != null) {
            byte[] bytes = (username + ENCODE_DELIMETER + password).getBytes(); //String.getBytes()使用给定的 charset 将此 String 
                                                                                           //编码到 byte 序列,并将结果存储到新的 byte 数组对象
            int b;

            for (int n = 0; n < bytes.length; n++) {
                b = bytes[n] ^ (ENCODE_XORMASK + n);  // ^ 表示异或
                buf.append((char)(ENCODE_CHAR_OFFSET1 + (b & 0x0F))); //& 与
                buf.append((char)(ENCODE_CHAR_OFFSET2 + ((b >> 4) & 0x0F))); //>>右移
            }
        }
        return buf.toString();
    }

    private static String[] decodePasswordCookie( String cookieVal ) {

        // check that the cookie value isn't null or zero-length
        if( cookieVal == null || cookieVal.length() <= 0 ) {
            return null;
        }

        // unrafel the cookie value
        char[] chars = cookieVal.toCharArray();
        byte[] bytes = new byte[chars.length / 2];
        int b;
        for (int n = 0, m = 0; n < bytes.length; n++) {
            b = chars[m++] - ENCODE_CHAR_OFFSET1;
                                        //m开始为0

           b |= (chars[m++] - ENCODE_CHAR_OFFSET2) << 4;
                                        //m开始为1

           bytes[n] = (byte)(b ^ (ENCODE_XORMASK + n));
        }
        cookieVal = new String(bytes);
        int	pos = cookieVal.indexOf(ENCODE_DELIMETER);
        String username = (pos < 0) ? "" : cookieVal.substring(0, pos);
        String password = (pos < 0) ? "" : cookieVal.substring(pos + 1);
         
         //添加一个输出测试
        System.out.println("用户名:"+username+"\n"+"密码:"+password);
        return new String[] {username, password};
    }

}
 

结果:

JkDkJkDkMmMkIhEhMh
用户名:chen
密码:chen

 

用表格分析encode

字符ASCIIENCODE_XORMASK+n二^三列的运算结果(异或)
c11000111011010     n=0111001
h11010001011011     n=1110011
e11001011011100111001
n11011101011101110011
\002 stx000001010111101011100
c11000111011111111100
h110100011000001000
e11001011100001100
n110111011000101100

 

 b = bytes[n] ^ (ENCODE_XORMASK + n);   //上表的第四列结果

//第一个append的是  b的低4位 +'A' =X
 buf.append((char)(ENCODE_CHAR_OFFSET1 + (b & 0x0F))); //& 与

 //第二个append的是 b的高4为 +'h' =Y
 buf.append((char)(ENCODE_CHAR_OFFSET2 + ((b >> 4) & 0x0F))); //>>右移

 

 decode译码过程(X-'A')|[(Y-'h')<<4] 最终再一个异或

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值