Genetic Algorithm password cracker in under 30 lines of code. Using Python and EasyGA

Daniel Wilczak
4 min readDec 9, 2020

A fun and easy way to learn about genetic algorithms by cracking a password.

EasyGA Python Package

So lets get straight to it.

pip3 install EasyGA

Now you have access to the EasyGA package. If you’ve never used it before I would check out the wiki or the getting started with EasyGA article that was created. They use the popular game among us to explain some of the novice features. Now were going to try and crack the password: EasyGA. Yup the worst password ever but it proves the point here.

Here is all the code we will need.

import EasyGA
import random
#Create the Genetic Algorithm
ga = EasyGA.GA()
password = input("""Please enter a word or sentence (Use only standard
characters such as letters, spaces and, punctuation marks): """)
ga.chromosome_length = len(password)ga.population_size = 50
ga.generation_goal = 1000
ga.fitness_goal = len(password)def password_fitness(chromosome):fitness = 0# For each gene in the chromosome
for gene, letter in zip(chromosome, password):
# If the gene letter matches the password
if gene.value == letter:
fitness += 1
return fitnessga.fitness_function_impl = password_fitness# Creates random genes utilizing the characters below
ga.gene_impl = lambda: random.choice([
"A","a","B","b","C","c","D","d","E","e",
"F","f","G","g","H","h","I","i","J","j",
"K","k","L","l","M","m","N","n","O","o",
"P","p","Q","q","R","r","S","s","T","t",
"U","u","V","v","W","w","X","x","Y","y",
"Z","z"," ",".","!","?"])
ga.evolve()#Print your default genetic algorithm
ga.print_generation()
ga.print_population()
#Prints a graph of the genetic algorithm
ga.graph.highest_value_chromosome()
ga.graph.show()

Output:

Current Generation     : 66
Chromosome - 0 [E][a][s][y][G][A] / Fitness = 6
Chromosome - 1 [E][a][g][y][G][A] / Fitness = 5
Chromosome - 2 [E][a][I][y][G][A] / Fitness = 5
Chromosome - 3 [E][a][I][y][G][A] / Fitness = 5
Chromosome - 4 [E][a][g][y][G][A] / Fitness = 5
Chromosome - 5 [E][a][I][y][G][A] / Fitness = 5
etc

Code Breakdown.

In this section we will talk about each section of the full example above.

import EasyGA
import random
#Create the Genetic Algorithm
ga = EasyGA.GA()

First, these are all the python import statements needed to get the code running. The 4th line of code is used to setup your ga class variable. This can be changed to any variable name wanted.

password = input("""Please enter a word or sentence (Use only standard characters such as letters, spaces and, punctuation marks): """)

Pretty straight forward input for you to put your password.

ga.chromosome_length = len(password)ga.population_size = 50
ga.generation_goal = 1000
ga.fitness_goal = len(password)

Now this is where we start set all the size attributes and what type of fitness goal we want. Now what is a fitness goal? Well genetic algorithms typically run off of a generation number. But EasyGA has made this even better by allow you to put a cap on the generations by using ga.generation_goal and allowing the genetic algorithm stop early if it reaches the desired fitness using ga.fitness_goal. You could also just inset a number here but we automated this.

def password_fitness(chromosome):    fitness = 0    # For each gene in the chromosome
for gene, letter in zip(chromosome, password):
# If the gene letter matches the password
if gene.value == letter:
fitness += 1
return fitnessga.fitness_function_impl = password_fitness

Now lets define our fitness function. To solve whether our chromosome matches our password were going to use a for loop and a zip function. The zip function allows us to loop through two lists at the same time. For more info on the zip function check out the w3school examples. If they do match then lets add one to that chromosomes overall fitness.

# Creates random genes utilizing the characters below
ga.gene_impl = lambda: random.choice([
"A","a","B","b","C","c","D","d","E","e",
"F","f","G","g","H","h","I","i","J","j",
"K","k","L","l","M","m","N","n","O","o",
"P","p","Q","q","R","r","S","s","T","t",
"U","u","V","v","W","w","X","x","Y","y",
"Z","z"," ",".","!","?"])

Now to create all the variations that a password could have we use the ga.gene_impl feature. This allows us to create all the chromosome genes using one simple function. For our example all the letters in the alphabet including capitals and a few symbols.

ga.evolve()

Now this is a crucial part in the genetic algorithm building process. Anything before this function is all setup and anything afterward is to show your completed genetic algorithm. The ga.evolve() function does all the magic we just setup. If you want to see every generation and its population then check out the EasyGA wiki. More specifically the ways to run.

#Print your default genetic algorithm
ga.print_generation()
ga.print_population()
#Prints a graph of the genetic algorithm
ga.graph.highest_value_chromosome()
ga.graph.show()

Now your almost finished. Lets just print out the generation number it finished on and the population to see your results. We also threw in a graph so that you can see graphically the progress your genetic algorithm took.

Thanks for reading,

Daniel Wilczak

--

--