SoFunction
Updated on 2024-11-20

Python implementation of permutations, cracking password algorithms examples

In this article, examples of Python implementation of permutations and combinations, password cracking algorithms. Shared for your reference, as follows:

Permutation and combination (password cracking)

1. Arrangement

(iterable,n)

Parameter one: the sequence to be aligned, the
Parameter 2: Number to be selected
The return is an iterator object, and each element in the iterator is a tuple

import itertools
# Concept: An arrangement of m (m ≤ n) elements from n different elements, arranged in a certain order, is called an arrangement of m elements from n elements. In particular, when m=n, the arrangement is called a Permutation.
'''
1 2 3 4
Suppose 3 numbers are taken from it
123
132
213
231
321
312
'''
#Requirement: Randomly take 3 numbers out of [1,2,3,4] 4 numbers to arrange them
mylist = list(([1,2,3,4], 3))
print(mylist)
print(len(mylist))
'''
Regularity Summary:
4 - 3 24
4 - 2 12
4 - 1 4
Number of possibilities for permutations: n! / (n-m)!
'''

2. Combination

(iterable,n)

Parameter 1: Iterable object
Parameter 2: Number to be selected
Return value: return one or two iterators, each element in the iterator is a tuple

import itertools
# Concept: from the m different elements, any n (n ≤ m) elements for a group, called from the m different elements out of the n elements for the combination of
'''
How many ways are there to combine 4 numbers out of 1 2 3 4 5?
'''
mylist = list(([1,2,3,4,5], 4))
print(mylist)
print(len(mylist))
'''
Regularity summary:
m n
5 - 5 1
5 - 4 5
5 - 3 10
5 - 2 10
5!
120/120 (m-n)!
120/24 (m-n)!
120/6(m-n)!
m!/(n!x(m-n)!)
'''

3. Arrangement and combination

(iterable,repeat=1)

Parameter one: iterable object, parameter two: number of repetitions, default is 1

import itertools
'''
_ _ _ _ _
'''
mylist = list(("0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm", repeat=6))
#You can try 10, there's a chance the computer will get stuck #
#Multi-threading doesn't work, not enough memory in the computer, it's useless.
#print(mylist)
print(len(mylist))

Extension: Nowadays, where passwords are concerned, they are generally encrypted, and commonly used encryption methods include MD5, RSA, DES, etc.

4. Crazy password cracking

Hurting the enemy by a thousand cracks

import time
import itertools
#mylist = list(("0123456789", repeat=10))
passwd = ("".join(x) for x in ("0123456789QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm", repeat=6))
#print(mylist)
#print(len(mylist))
while True:
  # Implement it directly first and then add exceptions
  try:
    str = next(passwd)
    (0.5)
    print(str)
  except StopIteration as e:
    break

Readers interested in more Python related content can check out this site's topic: theSummary of Python mathematical operations techniques》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniquesand thePython introductory and advanced classic tutorials

I hope that what I have said in this article will help you in Python programming.