无聊写个密码生成器

若要生成一个密码,有下列要求:

1.指定位数n(4<=N<=20)

2.符合密码复杂度要求,一个合格的密码必须含数字、大写字母、小写字母、特殊字符;

3.同一个密码里面出现的字符,不能在同一个密码里面重复。

4.以字母开头。

代码:

import string
import random
import re
def makepsd(n):
    s=string.ascii_letters
    d=string.digits
    o='~@#$%^&*?_'
    L=[]
    for x in d+s+o:
        L.append(x)
    random.shuffle(L)
    psd=''
    first_char=random.choice(s)
    chosen=set()
    chosen.add(first_char)
    newcode=''
    for x in range(1,n):
        newcode=random.choice(L)
        while newcode in chosen:
            newcode = random.choice(L)
        chosen.add(newcode)
        psd=psd+newcode
    psd=first_char+psd
    return psd


n=int(input("请输入密码的长度N(4<=N<=20):"))
mypsd=makepsd(n)
#print(mypsd)
def testpsd(password):
    pa=[]
    pa.append(re.compile(r'\d+'))
    pa.append(re.compile(r'[a-z]+'))
    pa.append(re.compile(r'[A-Z]+'))
    pa.append(re.compile(r'[~@#$%^&*?_]+'))
    isok=[]
    for p in pa:
        if re.search(p,mypsd):
            isok.append(True)
        else:
            isok.append(False)
    final_answer=True
    for item in isok:
        final_answer=final_answer and item
    return final_answer
#testpsd(mypsd)
#print(testpsd(mypsd))
while not testpsd(mypsd):
    mypsd=makepsd(n)
    #print(mypsd)
print("生成以字母开头,包含数字、大小写字母、特殊字符组成的不重复字符的%d位密码为:%s" %(n,mypsd))

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值