存档

‘Java’ 分类的存档

Java版Discuz! AuthCode

2010年3月9日 CanleiSky 3 条评论

package discuz;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Random;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class AuthCode {
    static BASE64Encoder encoder = new BASE64Encoder();
    static BASE64Decoder decoder = new BASE64Decoder();
    public enum DiscuzAuthcodeMode {
        Encode, Decode
    };

    public static void main(String[] args){
        System.out.print(”["+authcodeDecode("672d+9y5HQ0zqsONrBpSv5HwfTBASYjqVPyQ4O3RMN7xQcg", "soonyo")+"]“);
    }

    public static String CutString(String str, int startIndex, int length){
        if (startIndex >= 0) {
            if (length < 0) {
                length = length * -1;
                if(startIndex – length < 0) {
                    length = startIndex;
                    startIndex = 0;
                } else {
                    startIndex = startIndex – length;
                }
            }

            if (startIndex > str.length()) {
                return “”;
            }

        } else {
            if (length < 0) {
                return “”;
            } else {
                if (length + startIndex > 0) {
                    length = length + startIndex;
                    startIndex = 0;
                } else {
                    return “”;
                }
            }
        }

        if (str.length() – startIndex < length) {

            length = str.length() – startIndex;
        }

        return str.substring(startIndex, startIndex + length);
    }

    public static String CutString(String str, int startIndex) {
        return CutString(str, startIndex, str.length());
    }

    public static String MD5(String pass) {
        byte[] defaultBytes = pass.getBytes();
        try{
             MessageDigest algorithm = MessageDigest.getInstance(”MD5″);
             algorithm.reset();
             algorithm.update(defaultBytes);
             byte messageDigest[] = algorithm.digest();

             StringBuffer hexString = new StringBuffer();
             for (int i=0;i<messageDigest.length;i++) {
            String hex = Integer.toHexString(0xFF & messageDigest[i]);
            if(hex.length()==1)
            hexString.append(”0″ );

            hexString.append(hex);
             }
             return hexString.toString();
        }
        catch(NoSuchAlgorithmException nsae){
        }

        return “”;
    }

    public static boolean StrIsNullOrEmpty(String str) {
        //#if NET1
        if (str == null || str.trim().equals(”")) {
            return true;
        }

        return false;
    }

    static private byte[] GetKey(byte[] pass, int kLen) {
        byte[] mBox = new byte[kLen];

        for (int i = 0; i < kLen; i++) {
            mBox[i] = (byte) i;
        }

        int j = 0;
        for (int i = 0; i < kLen; i++) {

            j = (j + (int) ((mBox[i] + 256) % 256) + pass[i % pass.length])
                    % kLen;

            byte temp = mBox[i];
            mBox[i] = mBox[j];
            mBox[j] = temp;
        }

        return mBox;
    }

    public static String RandomString(int lens) {
        String[] CharArray = {”a”, “b”, “c”, “d”, “e”, “f”, “g”, “h”, “j”, “k”, “l”, “m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”, “w”, “x”, “y”, “z”, “0″ , “1″ , “2″ , “3″ , “4″ , “5″ , “6″ , “7″ , “8″ , “9″ };
        int clens = CharArray.length;
        String sCode = “”;
        Random random = new Random();
        for (int i = 0; i < lens; i++) {
            sCode += CharArray[Math.abs(random.nextInt(clens))];
        }
        return sCode;
    }

    public static String authcodeEncode(String source, String key, int expiry) {
        return authcode(source, key, DiscuzAuthcodeMode.Encode, expiry);

    }

    public static String authcodeEncode(String source, String key) {
        return authcode(source, key, DiscuzAuthcodeMode.Encode, 0);

    }

    public static String authcodeDecode(String source, String key) {
        return authcode(source, key, DiscuzAuthcodeMode.Decode, 0);

    }

    private static String authcode(String source, String key,
            DiscuzAuthcodeMode operation, int expiry) {
        try {
            if (source == null || key == null) {
                return “”;
            }

            int ckey_length = 4;
            String keya, keyb, keyc, cryptkey, result;

            key = MD5(key);

            keya = MD5(CutString(key, 0, 16));

            keyb = MD5(CutString(key, 16, 16));

            keyc = ckey_length > 0 ? (operation == DiscuzAuthcodeMode.Decode ? CutString(
                    source, 0, ckey_length)
                    : RandomString(ckey_length))
                    : “”;

            cryptkey = keya + MD5(keya + keyc);

            if (operation == DiscuzAuthcodeMode.Decode) {
                byte[] temp;
                temp = decoder.decodeBuffer(CutString(source, ckey_length));
                result = new String(RC4(temp, cryptkey));
                if (CutString(result, 10, 16).equals(CutString(MD5(CutString(result, 26) + keyb), 0, 16))) {
                    return CutString(result, 26);
                } else {
                    temp = decoder.decodeBuffer(CutString(source+”=”, ckey_length));
                    result = new String(RC4(temp, cryptkey));
                    if (CutString(result, 10, 16).equals(CutString(MD5(CutString(result, 26) + keyb), 0, 16))) {
                        return CutString(result, 26);
                    } else {
                        temp = decoder.decodeBuffer(CutString(source+”==”, ckey_length));
                        result = new String(RC4(temp, cryptkey));
                        if (CutString(result, 10, 16).equals(CutString(MD5(CutString(result, 26) + keyb), 0, 16))) {
                            return CutString(result, 26);
                        }else{
                            return “”;
                        }
                    }
                }
            } else {
                source = “0000000000″ + CutString(MD5(source + keyb), 0, 16)
                        + source;

                byte[] temp = RC4(source.getBytes(”GBK”), cryptkey);

                return keyc + encoder.encodeBuffer(temp);

            }
        } catch (Exception e) {
            return “”;
        }

    }

    private static byte[] RC4(byte[] input, String pass) {
        if (input == null || pass == null)
            return null;

        byte[] output = new byte[input.length];
        byte[] mBox = GetKey(pass.getBytes(), 256);

        int i = 0;
        int j = 0;

        for (int offset = 0; offset < input.length; offset++) {
            i = (i + 1) % mBox.length;
            j = (j + (int) ((mBox[i] + 256) % 256)) % mBox.length;

            byte temp = mBox[i];
            mBox[i] = mBox[j];
            mBox[j] = temp;
            byte a = input[offset];

            byte b = mBox[(toInt(mBox[i]) + toInt(mBox[j])) % mBox.length];

            output[offset] = (byte) ((int) a ^ (int) toInt(b));
        }

        return output;
    }

    public static int toInt(byte b) {
        return (int) ((b + 256) % 256);
    }

    public long getUnixTimestamp() {
        Calendar cal = Calendar.getInstance();
        return cal.getTimeInMillis() / 1000;
    }

}

记录一下,挺实有的函数,加解密从方式上看貌似与PHP版不同,但结果出来是一样滴~

分类: Java 标签:

正则零宽断言之双单标签混合解析

2010年1月8日 eays 没有评论

断言用来声明一个应该为真的事实。正则表达式中只有当断言为真时才会继续进行匹配。

正则表达式内容:
\{Tag:([a-zA-z0-9]*)\s([^}\/]*)\}(((?!\{\/Tag:)[\s\S])*?)\{/Tag:([a-zA-z0-9]*)\}

匹配字符串1:
{Tag:test param=”1″}test{/Tag:test}

和字符串2:
{Tag:test param=”1″}{Tag:test2 param=”2″/}{/Tag:test}

匹配字符串1非常简单,字符串2相对困难些,思考了一下午,看来只有零宽断言能解决,一时没体会到零宽断言的意思。

正则零宽断言部份:(((?!\{\/Tag:)[\s\S])*?)

中文说明::(((?!排除的字符)匹配所有表达式)无限次但尽可能少重复)

?!    是 负向零宽先行断言标志,(((?!\{\/Tag:)[\s\S])*?) 的意思是 断言匹配内容为{/Tag:外的所有内容,后跟\s\S括号*表示当断言为真时匹配所有内容。

分类: Java, JavaScript, PHP 标签:

Socket 编程

2009年9月5日 admin 没有评论

在windows上相信不少程序员都知道Socket,winsock应该就是windows socket的缩写。

Socket是个强大的东东,我也做过Socket编程,Socket之所以强大是因为它是基于TCP/IP协议的编程方式,如今品种繁多的协议也只不过是通过一定方式封装了TCP/IP协议的结果,我们可以使用Socket来编程,也就可以通过封装它而得到一个新的协议,所以Socket的灵活性是非常强的,基本可以满足任何网络通信应用。

分类: Java 标签: