腾讯登录加密方式的java实现

本文转载自 :http://hi.baidu.com/royripple/item/8bbf740fef8783cc75cd3ce0
腾讯登录加密方式的java实现,使用这个就不用再用jsEngine来做MD5加密
2012-6-13号更新可用。
另外,登录链接已改成如下:
get=new HttpGet("http://ptlogin2.qq.com/login?ptlang=2052&u="+Config.USERNAME+"&p="+pass+"&verifycode="+checkNum[1]+"&low_login_enable=1&low_login_hour=720&aid=46000101&u1=http%3A%2F%2Ft.qq.com&ptredirect=1&h=1&from_ui=1&dumy=&fp=loginerroralert&action=1-2-11125&g=1&t=1&dummy=");

成功返回:
ptuiCB('0','0','http://t.qq.com','1','登录成功!', 'roy');

========================================================
加密代码:
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;

public class MD5Security {
public static final String HEXSTRING = "0123456789ABCDEF";

    public static String md5(String originalText) throws Exception {
        byte buf[] = originalText.getBytes("ISO-8859-1");
        StringBuffer hexString = new StringBuffer();
        String result = "";
        String digit = "";

        try {
            MessageDigest algorithm = MessageDigest.getInstance("MD5");
            algorithm.reset();
            algorithm.update(buf);

            byte[] digest = algorithm.digest();

            for (int i = 0; i < digest.length; i++) {
                digit = Integer.toHexString(0xFF & digest[i]);

                if (digit.length() == 1) {
                    digit = "0" + digit;
                }

                hexString.append(digit);
            }

            result = hexString.toString();
        } catch (Exception ex) {
            result = "";
        }

        return result.toUpperCase();
    }

    public static String hexchar2bin(String md5str) throws UnsupportedEncodingException
    {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(md5str.length() / 2);

        for (int i = 0; i < md5str.length(); i = i + 2)
        {
            baos.write((HEXSTRING.indexOf(md5str.charAt(i)) << 4 |
                    HEXSTRING.indexOf(md5str.charAt(i + 1))));
        }

        return new String(baos.toByteArray(), "ISO-8859-1");
    }

    /**
     *
     * @param qq http://check.ptlogin2.qq.com/check?uin={0}&appid=15000101&r={1} 返回的第三个值
     * @param password QQ密码
     * @param verifycode 验证码
     * @return 加密后的密码
     * @throws UnsupportedEncodingException
     * @throws Exception
     */
    public static String GetPassword(String qq,String password, String verifycode) throws Exception{

        String P = hexchar2bin(md5(password));
        String U = md5(P + hexchar2bin(qq.replace("\\x", "").toUpperCase()));
        String V = md5(U + verifycode.toUpperCase());

        return V;
    }
        
        public static void main(String[] args) throws Exception {
            System.out.println(GetPassword("\\x00\\x00\\x00\\x00\\x90\\xbe\\x76\\xce","royripple1989082","!1VM"));
            //System.out.println(md5("\\x00\\x00\\x00\\x00\\x90\\xbe\\x76\\xce"));
       }
}

你可能感兴趣的:(java,MD5加密,腾讯登录)