首页 > 代码库 > Python 生成账号密码算法

Python 生成账号密码算法

有个需求,需要伪造跟用户行为非常类似的账号密码,而且需要一个阀值控制伪造的数量。

在这需求上,还有一个就是需要控制生成的比率、跳出率不能过高或者太低。

对此就随手用python写了一个,bug不知道有木有,没有测,具体有兴趣可以去改改。

#coding:utf-8import randomclass create_data:    global zimu_xx,zimu_dx,number,teshu    # 生成26个英文字母,大小写    zimu_xx=[chr(i) for i in range(97,123)]    zimu_dx=[chr(i).upper() for i in range(97,123)]    # 生成随机的数字范围在(1-8000)    number=[i for i in range(1,8001)]    # 生成特殊字符串    teshu=[!,@,#,$,%,^,&,*,(,),_,+,-,=,|,},{,[,],;,",,,<,>,.,?,/]    def __init__(self):        self.u_1=0        self.u_2=0        self.u_3=0        self.u_4=0        self.p_1=0        self.p_2=0        self.p_3=0        self.p_4=0        self.p_5=0        self.p_6=0    def username(self):        # 随机判断标准        task=random.randint(0,10)        # 结果列表        results_all=[]        # 字母结果        results_letters=[]        # 数字结果        results_digital=[]        try:                if (task>0 and task<3):                self.u_1=self.u_1+1                while len(results_letters)<4:                    results_letters.append(random.choice(zimu_xx))                results_all.append("".join(results_letters))                while True:                    if random.choice(number)<1500:                        results_all.append(random.choice(number))                        break                    else:                        continue                return results_all[0].encode(utf-8)+str(results_all[1])            elif (task>3 and task<6):                self.u_2=self.u_2+1                while len(results_letters)<5:                    results_letters.append(random.choice(zimu_xx))                results_all.append("".join(results_letters))                while True:                    if random.choice(number)>1500 and random.choice(number)<3000:                        results_all.append(random.choice(number))                        break                    else:                        continue                return results_all[0].encode(utf-8)+str(results_all[1])            elif (task>6 and task<9):                self.u_3=self.u_3+1                while len(results_letters)<6:                    results_letters.append(random.choice(zimu_xx))                results_all.append("".join(results_letters))                while True:                    if random.choice(number)>3000 and random.choice(number)<6000:                        results_all.append(random.choice(number))                        break                    else:                        continue                return results_all[0].encode(utf-8)+str(results_all[1])            else:                self.u_4=self.u_4+1                temp=[]                while len(results_letters)<3:                    results_letters.append(random.choice(zimu_xx))                results_all.append("".join(results_letters))                while True:                    if random.choice(number)>6000 and random.choice(number)<8000:                        results_all.append(random.choice(number))                        break                    else:                        continue                while len(temp)<3:                    temp.append(random.choice(results_letters))                results_all.append("".join(temp))                return results_all[0]+str(results_all[1])+results_all[2]                    except IndexError,e:            pass    def passwd(self):        # 随机判断标准        task=random.randint(0,15)        # 结果列表        results_all=[]        # 字母结果(大写)        results_letters_big=[]        # 字母结果(小写)        results_letters_small=[]        # 数字结果        results_digital=[]        # 特殊字符        results_special_characters=[]        try:                if (task>0 and task<3):                self.p_1=self.p_1+1                while len(results_letters_big)<3:                    results_letters_big.append(random.choice(zimu_dx))                while len(results_letters_small)<3:                    results_letters_small.append(random.choice(zimu_xx))                results_all.append("".join(results_letters_big))                results_all.append("".join(results_letters_small))                while len(results_special_characters)<3:                    results_special_characters.append(random.choice(teshu))                results_all.append("".join(results_special_characters))                while True:                    test_num=[i for i in range(200,800)]                    if random.choice(test_num)>300 and random.choice(test_num)<600:                        results_all.append(random.choice(test_num))                        break                    else:                        continue                return results_all[0]+results_all[1]+results_all[2]+str(results_all[3])            elif (task>3 and task<4):                self.p_2=self.p_2+1                while len(results_letters_big)<4:                    results_letters_big.append(random.choice(zimu_dx))                while len(results_letters_small)<3:                    results_letters_small.append(random.choice(zimu_xx))                results_all.append("".join(results_letters_big))                results_all.append("".join(results_letters_small))                while len(results_special_characters)<4:                    results_special_characters.append(random.choice(teshu))                results_all.append("".join(results_special_characters))                while True:                    test_num=[i for i in range(500,1400)]                    if random.choice(test_num)>600 and random.choice(test_num)<1200:                        results_all.append(random.choice(test_num))                        break                    else:                        continue                return results_all[0]+results_all[2]+results_all[1]+str(results_all[3])            elif (task>4 and task<9):                self.p_3=self.p_3+1                while len(results_letters_big)<3:                    results_letters_big.append(random.choice(zimu_dx))                while len(results_letters_small)<4:                    results_letters_small.append(random.choice(zimu_xx))                results_all.append("".join(results_letters_big))                results_all.append("".join(results_letters_small))                while True:                    test_num=[i for i in range(1100,2600)]                    if random.choice(test_num)>1200 and random.choice(test_num)<2400:                        results_all.append(random.choice(test_num))                        break                    else:                        continue                return results_all[1]+results_all[0]+str(results_all[2])            elif (task>9 and task<12):                self.p_4=self.p_4+1                while len(results_letters_big)<3:                    results_letters_big.append(random.choice(zimu_dx))                while len(results_letters_small)<4:                    results_letters_small.append(random.choice(zimu_xx))                results_all.append("".join(results_letters_big))                results_all.append("".join(results_letters_small))                while True:                    test_num=[i for i in range(2300,5000)]                    if random.choice(test_num)>2400 and random.choice(test_num)<5000:                        results_all.append(random.choice(test_num))                        break                    else:                        continue                return str(results_all[2])+results_all[1]+results_all[0]            elif (task>12 and task<15):                self.p_5=self.p_5+1                while len(results_letters_big)<4:                    results_letters_big.append(random.choice(zimu_dx))                while len(results_letters_small)<5:                    results_letters_small.append(random.choice(zimu_xx))                results_all.append("".join(results_letters_big))                results_all.append("".join(results_letters_small))                while True:                    test_num=[i for i in range(4900,10000)]                    if random.choice(test_num)>5000 and random.choice(test_num)<10000:                        results_all.append(random.choice(test_num))                        break                    else:                        continue                return str(results_all[2])+results_all[0]+results_all[1]            else:                self.p_6=self.p_6+1                while len(results_letters_big)<5:                    results_letters_big.append(random.choice(zimu_dx))                while len(results_letters_small)<4:                    results_letters_small.append(random.choice(zimu_xx))                results_all.append("".join(results_letters_big))                results_all.append("".join(results_letters_small))                return results_all[0]+results_all[1]        except IndexError,e:            pass    def count_data(self):        print "生成100组账号密码的概率比例:"        print "========账号类比率======="        print "方案1: %s%% 方案2: %s%% 方案3: %s%% 方案4: %s%%" % (self.u_1,self.u_2,self.u_3,self.u_4)        print "========密码类比率======="        print "方案1: %s%% 方案2: %s%% 方案3: %s%% 方案4: %s%% 方案5: %s%% 方案6: %s%%" % (self.p_1,self.p_2,self.p_3,self.p_4,self.p_5,self.p_6)if __name__==__main__:    t=create_data()    for i in range(0,100):        print "生成账号: %s 密码: %s" % (t.username(),t.passwd())    t.count_data()