Learning about Caesar Cipher

Caesar Cipher is a simple encryption technique that shifts the alphabet by a certain number of characters.
cryptography
Author

Daniel Fat

Published

March 22, 2020

The action of a Caesar cipher is to replace each plaintext letter with a different one a fixed number of places down the alphabet. The cipher illustrated here uses a left shift of three, so that (for example) each occurrence of E in the plaintext becomes B in the ciphertext. In cryptography, a Caesar cipher, also known as Caesar’s cipher, the shift cipher, Caesar’s code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a left shift of 3, D would be replaced by A, E would become B, and so on. The method is named after Julius Caesar, who used it in his private correspondence.

The encryption step performed by a Caesar cipher is often incorporated as part of more complex schemes, such as the Vigenère cipher, and still has modern application in the ROT13 system. As with all single-alphabet substitution ciphers, the Caesar cipher is easily broken and in modern practice offers essentially no communications security.

We want to use this package to generate random texts

Code
try:
    import lorem
except:
    !pip install lorem
    import lorem

now everytime we will run this cell the s variable will have a random sentence in latin

Code
s = lorem.sentence()
s
'Tempora neque est magnam numquam.'

Using this function we and the shift parameter we can rotate the ASCII code for each letter in the sentence

ASCII stands for American Standard Code for Information Interchange. Computers can only understand numbers, so an ASCII code is the numerical representation of a character such as ‘a’ or ‘@’ or an action of some sort. ASCII was developed a long time ago and now the non-printing characters are rarely used for their original purpose. Below is the ASCII character table and this includes descriptions of the first 32 non-printing characters. ASCII was actually designed for use with teletypes and so the descriptions are somewhat obscure. If someone says they want your CV however in ASCII format, all this means is they want ‘plain’ text with no formatting such as tabs, bold or underscoring - the raw format that any computer can understand. This is usually so they can easily import the file into their own applications without issues. Notepad.exe creates ASCII text, or in MS Word you can save a file as ‘text only’

I am sure there might be multiple implementations of this function but here is mine

Code
def caesar(text,shift=3):
    def to_cipher(x,shift):
        x = ord(x)
        if x <= 122 and x >= 93:
            if (x + shift) >= 122:
                return chr(x + shift - 122 + 93)
            else:
                return chr(x + shift)
        if x <= 90 and x >= 65:
            if (x + shift) > 90:
                return chr(x + shift - 90 + 65)
            else:
                return chr(x + shift)
        
        return str(x + shift)

    return ''.join([ to_cipher(x,shift) for x in list(text)])
Code
caesar(s,25)
'Tailkn]57jamqa57aop57i]cj]i57jqimq]i71'