Geek Culture
Published in

Geek Culture

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

--

--

--

A new tech publication by Start it up (https://medium.com/swlh).

Recommended from Medium

Quality nothing wide forward simply.

Bar Daily: How Hybrid Multi-Cloud Can Mitigate Current Supply Chain Woes

Pwning PHP: Type Juggling

Change JAVA Version in WSO2 Enterprise Integrator Docker image

Basic Kotlin: Function 2

How to Serialize and Deserialize Binary Search Tree

Demystifying cloud migration strategies

DevOps — The Next Best Solution for CRM

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
ofer shmueli

ofer shmueli

YouTube.com/fortitip

More from Medium

Uploading python packages to be used by anyone with python

http://pypi.org

Using Built-in Methods for User-defined Classes in Python.

Taming larger-than-RAM-data workflows: a Dask example

What are the Differences Between a Stack and a Heap?