Cryptography - The Most Basic Implementation

Cryptography - The Most Basic Implementation

Written : 2023-02-12 Last Update : 2023-02-12

Cryptography

Cryptography in very simple terms can be said is hiding information. We can use this to put some information public, but it is not useful to others, and can only be used by the person it is intended for.

You can imagine this as some code that you have set and it can only by understood by you or the people you have shared with.

Here we will look into a very basic implementation of cryptography and hiding information.

The System

Our system or algorithm, if you may, is simple.

We are going to revert and substitute the value of alphabets. So a shall be represented by z and z should be represented by a.

a = z
b = x
c = y
...
...
...
...
x = c
y = b
z = a

So our messages will be easily interchanged. A Plain text, Text will then become Gvcg.

Also See (opens in a new tab)

Character Dictionary

{ 
    'a' : 'z',
    'b' : 'y',
    'c' : 'x',
    'd' : 'w',
    'e' : 'v',
    'f' : 'u',
    'g' : 't',
    'h' : 's',
    'i' : 'r',
    'j' : 'q',
    'k' : 'p',
    'l' : 'o',
    'm' : 'n',
    'n' : 'm',
    'o' : 'l',
    'p' : 'k',
    'q' : 'j',
    'r' : 'i',
    's' : 'h',
    't' : 'g',
    'u' : 'f',
    'v' : 'e',
    'w' : 'd',
    'x' : 'c',
    'y' : 'b',
    'z' : 'a',
}

Python Code

There are obviously multiple ways to implement this, but since this is focused for absolute beginners , we will continue with a very basic way to implement it.

We will use concepts like Python Dictionaries to create key value pairs and cypher accordingly. In this way the code implementation will be simple and the data set will be easily visible.

alphabetdict = { 
    'a' : 'z',
    'b' : 'y',
    'c' : 'x',
    'd' : 'w',
    'e' : 'v',
    'f' : 'u',
    'g' : 't',
    'h' : 's',
    'i' : 'r',
    'j' : 'q',
    'k' : 'p',
    'l' : 'o',
    'm' : 'n',
    'n' : 'm',
    'o' : 'l',
    'p' : 'k',
    'q' : 'j',
    'r' : 'i',
    's' : 'h',
    't' : 'g',
    'u' : 'f',
    'v' : 'e',
    'w' : 'd',
    'x' : 'c',
    'y' : 'b',
    'z' : 'a',
}
 
def generateCypherText():
    data = input("Enter Plain text : ")
    cypherText = ''
    for i in data:
        if i.lower() in alphabetdict.keys():
            cypherText = cypherText + alphabetdict[i.lower()]
        else:
            cypherText = cypherText + i
    print("Your Cypher Text is : " + cypherText)
 
def generatePlainText():
    data = input("Enter Cypher text : ")
    plainText = ''
    for i in data:
        if i.lower() in alphabetdict.values():
            value = [x for x in alphabetdict if alphabetdict[x]== i]
            plainText = plainText + value[0]
        else:
            plainText = plainText + i
    print("Your Plain Text is : " + plainText)
 
 
print("1. Generate Cypher Text")
print("2. Generate Plain Text")
 
choice = int(input("> "))
 
if choice == 1:
    generateCypherText()
else:
    generatePlainText()