码迷,mamicode.com
首页 > 其他好文 > 详细

密码生成进阶

时间:2018-05-12 00:06:20      阅读:160      评论:0      收藏:0      [点我收藏+]

标签: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模板:

技术分享图片

密码生成进阶

标签:cooper

原文地址:http://blog.51cto.com/13118411/2115413

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!