Transposition Cipher Using Python Random Module
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 substitution algorithm( as used in Caesar cipher and vigenere cipher ) we replace one character with another by shifting its place in the alphabet with a pre-defined number of places
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
Let us use the python pseudo-random number generator module to take plain text and use transposition operation, with the key being the random seed ( the initial value, where the random generator start its math operations )
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,
As said, the seed value is an initial value that our random model uses at the start of the mathematical operation where it actually generates pseudo numbers.
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