密码生成进阶
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了密码生成进阶相关的知识,希望对你有一定的参考价值。
随机密码
定向生成
加盐
直接复制
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模板:
以上是关于密码生成进阶的主要内容,如果未能解决你的问题,请参考以下文章
我的C语言学习进阶之旅解决 Visual Studio 2019 报错:错误 C4996 ‘fscanf‘: This function or variable may be unsafe.(代码片段
我的Android进阶之旅NDK开发之在C++代码中使用Android Log打印日志,打印出C++的函数耗时以及代码片段耗时详情
我的Android进阶之旅关于Android平台获取文件的mime类型:为啥不传小写后缀名就获取不到mimeType?为啥android 4.4系统获取不到webp格式的mimeType呢?(代码片段
我的Android进阶之旅关于Android平台获取文件的mime类型:为啥不传小写后缀名就获取不到mimeType?为啥android 4.4系统获取不到webp格式的mimeType呢?(代码片段