1. 程式人生 > >密碼生成進階

密碼生成進階

cooper

隨機密碼

定向生成

加鹽

直接復制

import string 
import random

count = 8 
str_from = string.ascii_letters + string.digits
"".join([random.choice(str_from) for _ in range(count)])
‘NLlZXbh9‘
import string 
import random

def random_passwd(count):
    "生成隨機密碼,位數=count"
    str_from = string.ascii_letters + string.digits
    return "".join([random.choice(str_from) for _ in range(count)])

print(random_passwd(5))
print(random_passwd(7))
uDuGe
Y0Z7VCK
import base64
s = input(‘passwd:‘)
ss = base64.b64encode(s.encode(‘utf-8‘))  # encode 編碼 給計算機看
ss.decode(‘utf-8‘)  # decode 解碼給人看
passwd:aaa

‘YWFh‘
import base64

def custom_pwd(count=6):
    origin_pwd = input(‘輸入初始密碼:‘)
    new_pwd = base64.b64encode(origin_pwd.encode(‘utf-8‘))
    return new_pwd.decode(‘utf-8‘)[:count]

print(‘生成密碼:‘,custom_pwd(8))
輸入初始密碼:abc
生成密碼: YWJj
import base64

def custom_pwd(count=8):
    "生成自定義密碼,長度為count"
    origin_pwd = input(‘輸入初始密碼:‘)
    new_pwd = base64.b64encode(origin_pwd.encode(‘utf-8‘))
    new_pwd = new_pwd.decode(‘utf-8‘)[:count]
    if len(new_pwd) < count:
        new_pwd = new_pwd.ljust(count,‘1‘)
    return new_pwd

print(‘生成密碼:‘,custom_pwd(8))
輸入初始密碼:
生成密碼: 11111111
import base64

def custom_pwd(count=8):
    "生成自定義密碼,長度為count"
    origin_pwd = input(‘輸入初始密碼:‘)
    new_pwd = base64.b64encode(origin_pwd.encode(‘utf-8‘))
    new_pwd = new_pwd.decode(‘utf-8‘)[:count]
    if len(new_pwd) < count:
        new_pwd = new_pwd.ljust(count,‘1‘)
    return new_pwd[:count]

print(‘生成密碼:‘,custom_pwd(8))
輸入初始密碼:aaabbbcccddd
生成密碼: YWFhYmJi
import base64

def custom_pwd(origin_pwd,count=8):
    "生成自定義密碼,長度為count"
#    origin_pwd = input(‘輸入初始密碼:‘)
    new_pwd = base64.b64encode(origin_pwd.encode(‘utf-8‘))
    new_pwd = new_pwd.decode(‘utf-8‘)[:count]
    if len(new_pwd) < count:
        new_pwd = new_pwd.ljust(count,‘1‘)
    return new_pwd[:count]

print(‘生成密碼:‘,custom_pwd(‘aabbcc‘))
生成密碼: YWFiYmNj
import base64

def custom_pwd(origin_pwd,salt=‘coop‘,count=8):
    print(‘before salt:‘,origin_pwd)
    new_pwd = base64.b64encode((origin_pwd + salt).encode(‘utf-8‘))
    new_pwd = new_pwd.decode(‘utf -8‘)
    if len(new_pwd) < count:
        new_pwd.ljust(count,‘a‘)
    return new_pwd[3:3+count]

print(‘生成的密碼:‘,custom_pwd(‘abcdefghi‘))
before salt: abcdefghi
生成的密碼: jZGVmZ2h
import base64

def custom_pwd(origin_pwd,salt=‘‘,count=8):
    print(‘before salt:‘,origin_pwd)
    new_pwd = base64.b64encode((origin_pwd + salt).encode(‘utf-8‘))
    new_pwd = new_pwd.decode(‘utf -8‘)
    if len(new_pwd) < count:
        new_pwd.ljust(count,‘a‘)
    return new_pwd[3:3+count]

print(‘生成的密碼:‘,custom_pwd(‘abcdefghi‘))
before salt: abcdefghi
生成的密碼: jZGVmZ2h
import base64

def custom_pwd(origin_pwd,salt=‘coop‘,count=8,offset=4):
    print(‘before salt:‘,origin_pwd)
    new_pwd = base64.b64encode((origin_pwd + salt).encode(‘utf-8‘))
    new_pwd = new_pwd.decode(‘utf -8‘)
    if len(new_pwd) < count:
        new_pwd.ljust(count,‘a‘)
    return new_pwd[offset:offset+count]

print(‘生成的密碼:‘,custom_pwd(‘abcdefghi‘))
before salt: abcdefghi
生成的密碼: ZGVmZ2hp
import base64
import pyperclip
"""
知識點:
base64
encode 
decode
直接復制到剪切板
函數演變
代碼規範
"""

def custom_pwd(origin_pwd, salt=‘coop‘, count=8, offset=4):
    print(‘before salt:‘,origin_pwd)
    new_pwd = base64.b64encode((origin_pwd + salt).encode(‘utf-8‘))
    new_pwd = new_pwd.decode(‘utf -8‘)
    if len(new_pwd) < count:
        new_pwd.ljust(count,‘a‘)

    pyperclip.copy(new_pwd[offset:offset + count])
    return ‘密碼在剪切板上‘

print(‘生成的密碼:‘,custom_pwd(‘abcdefghi‘))  #ZGVmZ2hp 剪切板上的密碼
before salt: abcdefghi
生成的密碼: 密碼在剪切板上

    導入pyerclip模板:

技術分享圖片

密碼生成進階