Transposition Cipher Using Python Random Module

ofer shmueli
Apr 2, 2021 · 3 min read

In classic cryptography, we have 2 operations that are used throughout the history of hiding messages and turning plain text into a cipher message

Substitution and transposition

In the transposition algorithm, As the name implies, the plain text is re-ordered, we change the order of elements. But the character stays the same

We will start and import the random module. We will only use the random. sample function in it, as it allows us to randomize strings

Now create a variable that is a Boolean. And let’s use a while loop. So that we can actually continue our code again and again.

We will need user input, once for the message itself and the second for the key

let’s just create that

The first input “what is your message” the second one “What is your Key”

Now the key is the most interesting part since our key is actually the seed value,

And now let’s set our random seed, The user input for the key is saved as a variable we will place that just use our key

Moving on, we will use the random.sample() function itself, which takes two parameters. The first one is a list or a string, which in our case is the message. that we have just entered. And the second one is the length of the samples we want, we need the same number of samples as our string holds. the same number of letters. That’s why we will use the length of the message — len(message).

The last line of code is to print the encrypted message,

Now run your script.

What is your message?

The message will “attackatdawn”

What is the key?

The key will be 9.

Our ciphered message is “akaatcdwtatn”

Let’s use another random seed key

Geek Culture

Proud to geek out. Follow to join our +1.5M monthly readers.