webQQ协议——密码加密

密码的加密公式:  p = md5(md5(hexToBin(md5(Password)) + hexToBin(QQNumber))) + verifyCode.upper()) 

2014/06/28修改。。

没什么大变化, 就把整个类变成静态函数的集合而已。。。


PSWEncrypt.py

import hashlib;

class PSWEncrypt():
    def __init__(self):
        pass;
     
    @staticmethod
    def _userToBin(user):
        return PSWEncrypt._hex2bin(hex(int(user))[2:].zfill(16));
      
    @staticmethod
    def encrypt(user, psw, verifyCode):
        binUser = PSWEncrypt._userToBin(user);
        psw_1 = PSWEncrypt._md5Encrypt_1(psw);
        
        psw_1 = PSWEncrypt._hex2bin(psw_1);
        psw_2 = PSWEncrypt._md5Encrypt_2(psw_1, binUser);
        psw_3 = PSWEncrypt._md5Encrypt_3(psw_2, verifyCode);
        
        return psw_3;        
        
    @staticmethod
    def _hex2bin(string):                #字符串转bytes数组
        length = len(string);
        tmp = [];
        for i in range(0, length, 2):
            tmp.append(int("0x" + string[i:i + 2], base = 16));
        return bytes(tmp);
       
    @staticmethod
    def _md5Encrypt_1(psw):              #密码的第一次加密
        md5 = hashlib.md5();
        md5.update(psw.encode("ISO-8859-1"));
        return md5.hexdigest().upper();
    
    @staticmethod
    def _md5Encrypt_2(psw, user):        #密码的第二次加密
        md5 = hashlib.md5();
        md5.update(psw + user);
        return md5.hexdigest().upper();
    
    @staticmethod
    def _md5Encrypt_3(psw, verifyCode):  #密码的第三次加密
        md5 = hashlib.md5();
        md5.update((psw + verifyCode.upper()).encode("ISO-8859-1"));
        return md5.hexdigest().upper();


#--------------------------------------------(分割线)----------------------------------------------------------------------------------

pswEncrypt.py

"""
    参考:http://www.10qf.com/thread-13-1-1.html
         http://www.cnblogs.com/uu102/archive/2012/09/16/2687959.html
         http://www.219.me/posts/1729
         还有一个是主要参考的。。找不到地址了。。
"""

import hashlib;

class encryptPsw():
    def __init__(self):
        pass;
     
    def _userToBin(self, user):
        return self._hex2bin(hex(int(user))[2:].zfill(16));
        
    def encrypt(self, user, psw, verifyCode):
        binUser = self._userToBin(user);
        psw_1 = self._md5Encrypt_1(psw);
        
        psw_1 = self._hex2bin(psw_1);
        psw_2 = self._md5Encrypt_2(psw_1, binUser);
        psw_3 = self._md5Encrypt_3(psw_2, verifyCode);
        
        return psw_3;        
        
    def _hex2bin(self, string):                #字符串转bytes数组
        length = len(string);
        tmp = [];
        for i in range(0, length, 2):
            tmp.append(int("0x" + string[i:i + 2], base = 16));
        return bytes(tmp);
       
    def _md5Encrypt_1(self, psw):              #密码的第一次加密
        md5 = hashlib.md5();
        md5.update(psw.encode("ISO-8859-1"));
        return md5.hexdigest().upper();
        
    def _md5Encrypt_2(self, psw, user):        #密码的第二次加密
        md5 = hashlib.md5();
        md5.update(psw + user);
        return md5.hexdigest().upper();
    
    def _md5Encrypt_3(self, psw, verifyCode):  #密码的第三次加密
        md5 = hashlib.md5();
        md5.update((psw + verifyCode.upper()).encode("ISO-8859-1"));
        return md5.hexdigest().upper();
        
        
def main():
    a = encryptPsw();
    psw = a.encrypt("Q号", "密码", "验证码");
    print(psw);
    
if(__name__ == "__main__"):
    main();

以下是几组测试用例。。。要改为其他语言的可以对比下自己是否写对了。。

QQ               密码              验证码          p值
123456           test              !xyz            2418704E95EBDC8EBF7D963CCAB490BE
987654321        12345678          !abc            C8FD933322E69F8CC5531D8693A10DE7
987654321        12345678          xyzc            A498CB34757D1257CC1B8462870D9948





你可能感兴趣的:(webQQ,python,密码加密,python)