python 穷举指定长度的密码例子

本程序可根据给定的字符字典,穷举指定长度的所有字符串:

def get_pwd(str, num):
  if(num == 1):
   for x in str:
    yield x
  else:
   for x in str:
    for y in get_pwd(str, num-1):
     yield x+y

strKey="abc"
for x in get_pwd(strKey,3):
 print x

结果:

aaa
aab
aac
aba
abb
abc
aca
acb
acc
baa
bab
bac
bba
bbb
bbc
bca
bcb
bcc
caa
cab
cac
cba
cbb
cbc
cca
ccb
ccc

本程序占用内存小,生成速度快,欢迎尝试!!!

补充知识:Python 穷举法, 二分法 与牛顿-拉夫逊方法求解平方根的性能对比

穷举法, 二分法 与牛顿-拉夫逊方法求解平方根的优劣,从左到右依次递优。

经过测试,穷举法基本超过 1 分钟,还没有出数据;

二分法只要区区1秒不到就出结果了。

牛顿-拉夫逊是秒出,没有任何的停顿。

numberTarget =int(input("Please enter a number:"))
numberSqureRoot = 0
while(numberSqureRoot<abs(numberTarget)):
 if numberSqureRoot**2 >= abs(numberTarget):
  break
 numberSqureRoot = numberSqureRoot + 1

if numberSqureRoot**2 != numberTarget:
 print("Your number %s is not a perfect squre, the square root is %s " % ( numberTarget,numberSqureRoot) )
else:
 if numberTarget < 0 :
  numberSqureRoot = -numberSqureRoot
 print("Your number %s is a perfect squre, the square root is %s " % ( numberTarget, numberSqureRoot))

print("now we begin to calculate the binary search...")

numberTarget=int(input("Please enter the number for binary search..."))
numberSqureRoot = 0

lowValue = 0.0
highValue=numberTarget*1.0

epsilon = 0.01
numberSqureRoot = (highValue + lowValue)/2

while abs(numberSqureRoot**2 - numberTarget) >=epsilon:
 print("lowValue:%s, highValue:%s, currentValue:%s"%(lowValue,highValue,numberSqureRoot))
 if numberSqureRoot**2<numberTarget:
  lowValue=numberSqureRoot
 else:
  highValue=numberSqureRoot
 numberSqureRoot = (lowValue+highValue) /2

print("The number %s has the squre root as %s " %(numberTarget,numberSqureRoot))

print("now we begin to calculate the newTon search...")

numberTarget=int(input("Please enter the number for newTon search..."))
numberSqureRoot = 0

epsilon = 0.01
k=numberTarget
numberSqureRoot = k/2.0

while( abs(numberSqureRoot*numberSqureRoot - k)>=epsilon):
 numberSqureRoot=numberSqureRoot-(((numberSqureRoot**2) - k)/(2*numberSqureRoot))

print("squre root of %s is %s " %(numberTarget,numberSqureRoot))

以上这篇python 穷举指定长度的密码例子就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • Python 线性回归分析以及评价指标详解

    废话不多说,直接上代码吧! """ # 利用 diabetes数据集来学习线性回归 # diabetes 是一个关于糖尿病的数据集, 该数据集包括442个病人的生理数据及一年以后的病情发展情况. # 数据集中的特征值总共10项, 如下: # 年龄 # 性别 #体质指数 #血压 #s1,s2,s3,s4,s4,s6 (六种血清的化验数据) #但请注意,以上的数据是经过特殊处理, 10个数据中的每个都做了均值中心化处理,然后又用标准差乘以个体数量调整了数值范围. #验证就会发现任

  • python简单的三元一次方程求解实例

    我就废话不多说了,直接看代码吧! import re lt = [] d = {} for i in range(3): a = input('请输入第%d个三元式'%(i + 1)) st = a.split("=") r = re.compile('(-?\d?)[xyz]') b = re.findall(r, st[0]) print(b) for j in range(3): if b[j] == "": b[j] = 1 if b[j] == '-':

  • Django REST framwork的权限验证实例

    在这里插入代码片# Django REST framwork的权限验证 一.用户是否登录 (1)判断用户是否登录: permission_classes = (IsAuthenticated, ) 注意:permission_classes设置的是:验证的是用户是否登录.用户是否可以操作该数据等的权限: 权限组合方式,目前支持:与&(and) 或|(or) 非~(not) 例如:permission_classes = (SecAdminPermission | AudAdminPermissi

  • python 穷举指定长度的密码例子

    本程序可根据给定的字符字典,穷举指定长度的所有字符串: def get_pwd(str, num): if(num == 1): for x in str: yield x else: for x in str: for y in get_pwd(str, num-1): yield x+y strKey="abc" for x in get_pwd(strKey,3): print x 结果: aaa aab aac aba abb abc aca acb acc baa bab b

  • python随机生成指定长度密码的方法

    本文实例讲述了python随机生成指定长度密码的方法.分享给大家供大家参考.具体如下: 下面的python代码通过对各种字符进行随机组合生成一个指定长度的随机密码 python中的string对象有几个常用的方法用来输出各种不同的字符: string.ascii_letters 输出ascii码的所有字符 string.digits 输出 '0123456789'. string.punctuation ascii中的标点符号 print string.ascii_letters print s

  • JAVA编程实现随机生成指定长度的密码功能【大小写和数字组合】

    本文实例讲述了JAVA编程实现随机生成指定长度的密码功能.分享给大家供大家参考,具体如下: import java.util.Random; public class PassWordCreate { /** * 获得密码 * @param len 密码长度 * @return */ public String createPassWord(int len){ int random = this.createRandomInt(); return this.createPassWord(rand

  • python3实现暴力穷举博客园密码

    我之前想写路由器的密码暴力破解器,我手上只有极路由,发现极路由有安全限制,只能允许连续10密码错误,所以我改拿博客园练手. 博客园的博客有个功能是给博文设置一个密码,输入正确的密码才能看到文章的内容.经过测试发现这个密码验证功能,既没有验证码也没有提交频率的限制, 要写这个针对博客园的密码暴力破解器模型会非常简单,很好实现. 比如打开这个博文,会显示一个密码输入框:http://www.cnblogs.com/post/readauth?url=/muer/archive/2011/11/27/

  • python实现对指定字符串补足固定长度倍数截断输出的方法

    简单的小练习,注意考虑全可能就行,下面是实现: #!usr/bin/env python #encoding:utf-8 ''' __Author__:沂水寒城 功能:•连续输入字符串,请按长度为8拆分每个字符串后输出到新的字符串数组: •长度不是8整数倍的字符串请在后面补数字0,空字符串不处理. ''' def func_test(): ''' 补足固定长度倍数输出 ''' a=raw_input() b=raw_input() len_a=len(a) len_b=len(b) a_list

  • python requests指定出口ip的例子

    爬虫需要,一个机器多个口,一个口多个ip,为轮询这些ip demo #coding=utf-8 import requests,sys,socket from requests_toolbelt.adapters import source reload(sys) sys.setdefaultencoding('utf-8') s = requests.Session() new_source = source.SourceAddressAdapter('192.168.4.2') s.moun

  • python print 格式化输出,动态指定长度的实现

    假如有一组字符,长度未知,进行格式化对其.首先算出所有字符中最长的.然后再进行格式化输出. print %s固定长度格式输出: print("%10s,%20s"%(A,B)) 如果长度不定,以变量len代替: print("%*s"%(len,A)) 其他操作待续 补充知识:Python 以指定宽度格式化输出(format) 当对一组数据输出的时候,我们有时需要输出以指定宽度,来使数据更清晰.这时我们可以用format来进行约束. mat = "{:20

  • Python中生成一个指定长度的随机字符串实现示例

    方法一: 定义一个函数,参数为所要生成随机字符串的长度.通过random.randint(a, b)方法得到随机数字,具体函数如下: def generate_random_str(randomlength=16): """ 生成一个指定长度的随机字符串 """ random_str = '' base_str = 'ABCDEFGHIGKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz0123456789' l

  • 教你利用Python破解ZIP或RAR文件密码

    一.破解原理 其实原理很简单,一句话概括就是「大力出奇迹」,Python 有两个压缩文件库:zipfile 和 rarfile,这两个库提供的解压缩方法 extractall()可以指定密码,这样的话首先生成一个密码字典(手动或用程序),然后依次尝试其中的密码,如果能够正常解压缩表示密码正确. 二.实验环境 本文采取的虚拟环境为 Pipenv. 库 zipfile:Python 标准库,使用时直接导入即可 rarfile:Python 第三方库 利用 Pipenv 安装 rarfile pipe

  • Python猜解网站数据库管理员密码的脚本

    目录 一.功能分析 二.思路分析 三.步骤实现 1)判断注入点 2)猜解长度 3)猜解数据 4)猜解脚本 一.功能分析 简单分析一下网站的功能,大致如下: 需要用户在地址栏中提交参数,根据参数中的id查询对应的用户信息. 如果id存在,则显示查询成功,比如 输入?id=1 如果id不存在,则页面空显示,比如输入 ?id=0(用户id不能是0或负数,id为0时,查询结果为空,会导致页面空显示) 如果数据库报错,页面也是空显示,比如输入?id=1’ 或 ?id=1"(参数中携带引号会导致数据库报错,

随机推荐