# The first stop of Python Programming - simple password dictionary generation

2022-05-15 06:04:05

Catalog

One 、 needed python modular

Two 、 scripting

3、 ... and 、 Advantages of the second method

## One 、 needed python modular

itemtools A brief understanding of the module is as follows ：

``````impor itertools

# product(*iterables,repeat=n)
# 1) When repeat=1 when , Is equivalent to ‘abcd’ and xy Are combined , But the sequence can only be ‘abcd’ stay ‘xy’ front
'''''''''
ite = itertools.product('abcd','xy',repeat=1)
list1 = list(ite)

print(f' The length is ：{len(list1)}')  #  The length is ：8
print(list1)    # [('a', 'x'), ('a', 'y'), ('b', 'x'), ('b', 'y'), ('c', 'x'), ('c', 'y'), ('d', 'x'), ('d', 'y')]
'''''''''

# 2） When repea=2 when , Is equivalent to ‘abcd’ A copy of , hold ‘abcd’ and ‘abcd’ Are combined
'''''''''
ite1 = itertools.product('abcd',repeat=2)
list2 = list(ite1)

print(f' The length is ：{len(list2)}')  #  The length is ：16
print(list2)    # [('a', 'a'), ('a', 'b'), ('a', 'c'), ('a', 'd'), ('b', 'a'), ('b', 'b'), ('b', 'c'), ('b', 'd'), ('c', 'a'), ('c', 'b'), ('c', 'c'), ('c', 'd'), ('d', 'a'), ('d', 'b'), ('d', 'c'), ('d', 'd')]
'''''''''``````

The following methods will also be used os、sys modular , You can learn from Baidu by yourself ;

## Two 、 scripting

1. Simple password dictionary generation script

'''''''''

explain ：

1）words Followed by the characters you want to randomly arrange and combine ;

2） The generated permutation and combination passwords will be saved in the locally generated pass.txt in

3）repeat The following number is the password that generates several digits

'''''''''

``````import itertools as ite
words = '[email protected]\$%^&*'
r = ite.product(words,repeat=3)
dic = open('pass.txt','a')
for i in r:
dic.write(''.join(i))
dic.write(''.join('\n'))

dic.close()

``````

2. Simple password dictionary generation script optimization

'''''''''

explain ：

1） After executing the script, enter the characters and password digits that need to be combined randomly ;

2） The generated permutation and combination passwords will be saved in the locally generated pass.txt in

3）repeat The following number is the password that generates several digits

'''''''''

``````import itertools as ite
import sys,os

words = sys.argv[1]
num = int(sys.argv[2])
path = str(os.path)
r = ite.product(words,repeat=num)

with open('pass.txt','a') as f:
for i in r:
f.write(''.join(i))
f.write(''.join('\n'))
print(' Generated by '+words+' Randomly composed '+str(num)+' Bit password dictionary ！')
print(' Password dictionary name ：pass.txt')
print(' Dictionary path ：',path)``````
``` perform ：
Execute at the terminal ：python .\test.py abc 3
```
`````` Generated by abc Randomly composed 3 Bit password dictionary ！
Password dictionary name ：pass.txt
Dictionary path ： <module 'ntpath' from 'D:\\Tools\\Python\\install\\lib\\ntpath.py'>``````

## 3、 ... and 、 Advantages of the second method

```# 1） After optimization, there is no need to modify the combined characters from the script
# 2） The optimized code is more concise

#  Beginners write their own , Understand that there will be better methods and optimization .```

More secure sharing , Please pay attention to 【 Security info】 WeChat official account ！