Skip to main content

A Spy's Whisper: Encryption & Decryption Of Messages Using Python

Project description

Agent James prepares to transmit vital information to his superiors. In the quest for heightened security, Agent James entrusted Python developers to fortify his message encryption. After exploring various ciphers, they decided to combine Route Cipher, Rail Fence Cipher, Substitution Cipher with custom cipher kk cipher. Now, armed with this amalgamation of ciphers, James can transmit vital information with confidence.

a) Route Cipher Encryption

Creating a Control Message To understand how to do this, create your own message and route cipher. Call this your control message:

• Number of columns = 4

• Number of rows = 5

• Start position = Bottom left

• Route = Alternating up and down columns

• Plaintext = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

• Ciphertext = 16 12 8 4 0 1 5 9 13 17 18 14 10 6 2 3 7 11 15 19

• Key = –1 2 –3 4

Using a numeric progression for the plaintext allows you to instantly tell whether you’ve gotten all or part of the decryption correct, at any place within the message

image

The key keeps track of both the order and direction of the route through the columns. The route doesn’t have to move through the columns in order. For instance, it can move down the first column, up the third, down the fourth, and finally up the second. Negative numbers mean you start at the bottom and read up a column; positive numbers mean the reverse. For the control message, the final key used in the program will be a list: [–1, 2, –3, 4]. This list will instruct the program to start reading up from the bottom of column 1, move to the top of column 2 and read down, move to the bottom of column 3 and read up, and move to the top of column 4 and read down. Note that you shouldn’t use 0 in keys because the users, being human, prefer to start counting at 1. Of course, Python prefers to start counting at 0, so you’ll need to subtract 1 from the key values behind the scenes. Everybody wins!

image

image

image

b) Rail Fence Cipher Encryption

Confederate officers and spies were pretty much on their own when it came to cryptography. This led to unsophisticated solutions, one of the favorites being the rail fence cipher, so named due to its resemblance to the zigzag pattern of a split-rail fence.

image

The rail fence is a simple-to-use transposition cipher, like the Union’s route cipher, but differs from the route cipher in that it transposes letters rather than words, making it more error-prone. And since the number of possible keys is much more restrictive than the number of paths through a route cipher, the rail fence cipher is much easier to “tear down.”

The Strategy

To Encrypt a message with rail fence cipher, follow the following steps image

After the plaintext is written, the spaces are removed, and all the letters are converted to uppercase (Step 2). Using uppercase is common convention in cryptography, as it obfuscates the presence of proper names and the beginning of sentences, giving a crypt analyst fewer clues for deciphering the message. The message is then written in stacked fashion, with every other letter below the previous letter and shifted over one space (Step 3). This is where the “rail fence” analogy becomes apparent. The first row is then written, followed immediately by the second row on the same line (Step 4), and then the letters are broken into groups of five to create the illusion of distinct words and to further confuse the crypt analyst (Step 5).

image

image

c) Substitution Cipher Encryption

Substitution cipher encryption involves replacing each plaintext letter with a different letter in a random way.

image

image

image

d) My Cipher Encryption

Replacing alphabets with particular values. image

image

image

Deciphering Danger: Race Against Time

Agent Sarah, receiving a crucial encrypted message from her trusted ally, Agent James, understands the urgency. With time ticking away, she diligently decrypts the message, knowing the gravity of the situation. Employing her expertise, Sarah tackles the complex ciphers — kk cipher, Substitution cipher, Rail Fence cipher, and Route cipher with precision and determination. Finally, with the decrypted message in hand, Sarah unveils the hidden truth within.

a) kk Cipher Decryption

Replacing alphabets to original alphabets based on kk cipher encryption

image

image

b) Substitution Cipher Decryption

Replacing alphabets with random interchange of alphabets.

image

image

image

c) Rail Fence Cipher Decryption

image

image

image

image

image

d) Route Cipher Decryption

Designing, Populating, and Depopulating the Matrix

You’ll input the ciphertext as a continuous string. For your program to unravel the route through this string, you’ll first need to build and populate a translation matrix. The ciphertext string is just the columns in the transposition matrix laid end to end, in the order they were read. And as there are five rows in the transposition matrix, every group of five elements in the ciphertext represents a separate column. You can represent this matrix with a list of lists:

image

The items in this new list now represent lists — with each list represent[1]ing a column — and the five elements in each list represent the rows that comprise that column. This is a little hard to see, so let’s print each of these nested lists on a separate line:

image

If you read each list left to right, starting at the top, you follow the trans[1]position route, which was up and down alternate columns. From Python’s point of view, the first column read is list-of-lists[0], and the starting point is list-of-lists[0][0]. Now, normalize the route by reading all columns in the same direction as the starting column (up). This requires reversing the order of elements in every other list, as shown in bold here:

image

A pattern emerges. If you start at the upper right and read down each column, ending at the lower left, the numbers are in numerical order; you’ve restored the plaintext! To replicate this, your script can loop through every nested list, removing the last item in that list and adding the item to a new string, until the Decoding American Civil War Ciphers 69 translation matrix has been emptied. The script will know from the key which nested lists it needs to reverse and the order in which to depopulate the matrix. The output will be a string of the restored plaintext:

image

image

image

image

With a sense of purpose and determination, Sarah transmits the decrypted message to her superiors, alerting them to the imminent danger.

Project details


Release history Release notifications | RSS feed

This version

1.0

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cipher_decipher-1.0.tar.gz (4.8 kB view details)

Uploaded Source

File details

Details for the file cipher_decipher-1.0.tar.gz.

File metadata

  • Download URL: cipher_decipher-1.0.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.13

File hashes

Hashes for cipher_decipher-1.0.tar.gz
Algorithm Hash digest
SHA256 103029c15052ec5cb3a7b1f9d03b6e0531d695e382547f7a6b18c632f9b1ba5e
MD5 0e575fd87590196f664ee64d0bcfff27
BLAKE2b-256 b515663316b6cebb328baec26a793339253e60395bddef336a29e4bbd0d4383e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page