首页 > 编程语言 >使用python生成随机密码

使用python生成随机密码

时间:2023-07-01 12:45:46浏览次数:41  
标签:python random filename 密码 随机 import password

使用python生成随机密码,密码长度13位,一般密码文件不能以?和! 开头的,需要将这两个开头的密码文件排除掉。

有两种方式。

第一种方式

import random
import string

# 定义密码长度
password_length = 13

# 定义密码字符集合
password_characters = string.ascii_letters + string.digits + string.punctuation

# 生成10个随机密码并写入文件
for i in range(10):
    # 生成随机密码
    password = ''.join(random.choice(password_characters) for i in range(password_length))

    # 确保密码不以'!'或'?'开头
    while password.startswith(('!', '?')):
        password = ''.join(random.choice(password_characters) for i in range(password_length))

    # 将密码写入文件
    filename = f"password_{i+1}.txt"
    with open(filename, 'w') as f:
        f.write(password)

 

第二种使用faker模块。

from faker import Faker
import random

fake = Faker()

# 生成10个随机密码
passwords = []
while len(passwords) < 10:
    password = fake.password(length=13)
    if not password.startswith(('!', '?')):
        passwords.append(password)

# 将密码写入文件
for i, password in enumerate(passwords):
    filename = f"password_{i+1}.txt"
    with open(filename, 'w') as f:
        f.write(password)

 

标签:python,random,filename,密码,随机,import,password
From: https://www.cnblogs.com/ken-yu/p/17519122.html

相关文章

  • 网络安全 | 密码基础知识介绍
    概述密码介绍安全问题保密性:对发送的消息进行获取完整性:对发送的消息进行篡改身份伪造:对发送的主体身份进行篡改,a发的消息,篡改为b发的行为抵赖:对发送的消息进行否认,丧失行为的可追溯性密码技术保密性完整性真实性不可否认性密码发展史密码起源:狼烟、虎符,基于“密语”古典密码:凯撒密......
  • Python-练脑系列-04依旧是数据结构
    前言......
  • 面向对象编程Python:类、对象和继承
    面向对象编程(Object-OrientedProgramming,简称OOP)是一种常用的编程范式,它将数据和操作数据的函数封装在一起,以创建对象。Python是一种支持面向对象编程的高级编程语言,它提供了类(class)、对象(object)和继承(inheritance)等概念,使得代码更加模块化、可维护性更高。本文将介绍Python中的......
  • python执行终端命令并获得输出结果
    兼容windows和linux的终端执行函数defshell_exec(cmd:str)->str:  """ 执行终端命令,输出终端打印结果 :paramcmd: :return: """  withos.popen(cmd)asfp:    bf=fp._stream.buffer.read()  out=bf.decode().strip()  retu......
  • 16.python-单例模式
    python-单例模式单例模式适用于需要共享对象的场景单例模式,也叫单子模式,是一种常用的软件设计模式。在应用这个模式时,单例对象的类必须保证只有一个实例存在。许多时候整个系统只需要拥有一个的全局对象,这样有利于我们协调系统整体的行为。比如在某个服务器程序中,该服务器的配......
  • python类与对象
    在Python中,类是一种用于创建对象的蓝图或模板。它们定义了对象的属性和方法。对象是类的实例化,它们是具体的、实际存在的实体。要定义一个类,可以使用class关键字,后面跟着类的名称。类名称通常使用首字母大写的驼峰命名法。下面是一个简单的类的示例:classPerson:def__init__(......
  • IDApython的学习
    IDApython的学习我的IDA情况:IDA7.7,idapython3.8这个可以作为文件导入和命令行内输入,我一般习惯命令行这里要注意是python不是IDC访问原数据idc.get_wide_byte(ea)//获取单字节,按整形解释idc.get_wide_word(ea)//获取双字节,按整形解释idc.get_wide_dword(ea)//获取......
  • python随笔
    一,安装模块windows环境下安装模块直接在CMD里面,运行命令:pipinstallparamiko, 删除命令,pipuninstallparamiko 用python连接linux,然后运行命令:importparamikohostname='192.168.1.112'username='root'password='123456'ssh=paramiko.SSHClient()ssh.set_miss......
  • python: more Layer Architecture and its Implementation in SQLite
    sqlite3:CREATETABLEDuStudentList( StudentId INTEGERPRIMARYKEYAUTOINCREMENT, StudentName TEXTNOTNULL, StudentNO TEXTNOTNULL, StudentBirthday DATETIME );Model:"""StudentListInfo.py学生类date2023-06-16edit:GeovinD......
  • python基本数据类型
    基本数据类型1、整型intage=182、浮点型floatsalary=1.53、字符串strname="mary"4、列表list   [ ],支持任意类型  [1,'jason',[1,2,3,4]]5、字典dic {"k":"v"} {'username':'mary','password':123}6、集合......