What is a Brute Force attack, and how to code one in Python

In my last tutorial I talked about what a Caesar cipher was, the history of it and how to encrypt and de-crypt one. This brief article will be a continuation from this where we build off the previous code and work on brute forcing the created encryption.

What is a Brute Force

So before you go and look at what the brute force code is doing you might be thinking; what is a brute force?

To picture this we can imagine a lock on a door without a key. To open the door you could simply pick up something and start smashing the lock (and hope it opens) or you could brute force the lock by using lock picks and picking the individual pins in the chamber.

An example of a simple brute force where we attempt to discover a hidden two digit number is below. Here we loop around all possible numbers from ‘0’ and stop when we hit the correct answer.

Hidden Number: 18
Brute Force Attempts: 00, 01, 02, 03, 04, 05, 06, 07, 08, 09, 10, 11, 12, 13, 14, 15, 16, 17, 18
Answer: 18

Python Code

The below code loops through the possibilities of the Caesar cipher starting from key 1 through to key 25. After this it outputs the shifted text with one string per line. The below is commented so that you can take the code one step at a time.


Image source: Python

Robert Sterio

I've been studying the field of Cyber Security for some time now and have grown a real passion for it as well as the related networking areas.

You may also like...