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

KWoC Project Report

GXChain Progress Report June.13-June.19

Set Up A Static Go Server With Docker For A React App

Moby Dock carrying the Go Gopher’s containers

C4 model and agility: how our community experimented with documenting architecture

Learn Android — Fundamentals

DEEP LEARNING : USING YOLO TO DETECT OBJECTS ON A VIDEO FILE(PYTHON TUTORIAL)

How to Resolve error during SSL Handshake with Remote Server?

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

Calculate Hadamard Product using Python — Linear Algebra

Python is the Programming language of the Year🏆

Pythonic Way 1: Intro, Range

Why and How to Write Frozen Dataclasses in Python