top of page
cosmodiumcs.png
  • Writer's pictureC0SM0

Caesar Cipher - CodeX

Updated: Mar 22, 2022

//The first project update for The Codex Project...


Hey Hackers! On November 3, 2020, I first came up with the idea to start The Codex Project. It would be a Python Cryptography Suite, capable of encrypting, decrypting, bruteforcing, encoding, and deciphering different encryptions, ciphers, encodings, and hashes. This article will deep dive into how we implemented the caesar cipher into the codex. However, if you wish to see the video progress, I will attach the first cyberlog for this project below.

 
 

//Understanding The Caesar Cipher:

Before I can explain what the Caesar Cipher is, I must explain what symmetric encryption is. Symmetric encryption [symmetric key encryption] is a ciphering algorithm that uses the same key to encrypt and decrypt plaintext.

 
 

The Caesar Cipher is a symmetric encryption ciphering process, that works by moving [shifitng] letters down the alphabet by a certain number [index]. It was named after Julius Caesar, who used it as a way to send messages amongst his army. Here is an example of the ciphering process, shifting one [KEY = 1] letter down the alphabet:


Plaintext - "HELLO"

Ciphertext - "IFMMP"


Our plaintext is the English word "hello". Each letter within our plaintext is shifted down one letter [shifted by the key] down the alphabet. "I" is one letter after "H", so with a key of one, the plaintext "H" is converted to an "I". This same process is applied to the other letters of the plaintext.


Note that special characters and numbers are ignored in the ciphering process

To help clarify the indexing of the cipher, still using our key of 1:


Plaintext Indexing:

A

B

C

1

2

3

Ciphertext Indexing: [shift key of one applied to plaintext]

B

C

D

1

2

3

In the above example, we can see each letter and its indexes. The plaintext indexing represents the standard alphabet, each index represents what numerical position the letter is in [going down the alphabet]. The ciphertext shows each letter, shifting one index down the alphabet.


//Algorithms:

The actual equations behind the Caesar Cipher are pretty simple:

  • Encryption:

C = (X + K) % 26

  • Decryption:

C = (X - K) % 26


C - alphabetical character

X - index of character

K - number of indexes that we need to replace [key]

26 - representing the 26 letters of the alphabet


Now as helpful as these equations are, we would need to modify them to work in a Python-based environment. After all, Python code and computers don't understand English alphabet indexing. Even if we used data structures like dictionaries, we would still have many problems in our code, efficiency, and accounting for upper and lowercase letters. So instead, we will use Unicode.


Unicode is the computer standard when it comes to handling text. Each letter and character is represented with a specific index. Where "A" has an Index of 65 and "a" has an index of 97. The 25 indexes after each of these letters would represent the rest of the alphabet [uppercase or lowercase].


So, let's modify our equation to work in Python. We will need to understand two functions:


ord(x) - matches character [x] to Unicode index

chr(x) - matches index [x] to Unicode character


Through these functions, we can apply this algorithmic equation to our Python code:

  • Encryption:

Uppercase:

	p += chr((ord(letter) + key - 65) % 26 + 65)

Lowercase:

	p += chr((ord(letter) + key - 97) % 26 + 65)
  • Decryption:

Uppercase:

	c += chr((ord(letter) - key - 65) % 26 + 65)

Lowercase:

	c += chr((ord(letter) - key - 65) % 26 + 65)

p - plaintext variable
c - ciphertext variable
letter - character to encrypt or decrypt
65,75 - accounting for Unicode indexing

//Python Encryption & Decryption:
We can easily take these algorithms and put them into a script. For the sake of this article, I will write some pseudo code on how the implementation works. But feel free to examine the code here.

//Encryption:
# symbols that can't be processed through the cipher
symbols= ['\n', '\t', ' ', '.', '?', '!', ',', '/', '\\', '<', '>', '|','[', ']', '{', '}', '@', '#', '$', '%', '^', '&', '*', '(', ')','-', '_', '=', '+', '`', '~', ':', ';', '"', "'", '0', '1', '2', '3','4', '5', '6', '7', '8', '9']

# encrypts content
def encrypt_caesar(plain_content, encryption_key, print_cnt):
    # output variable
    output = ''
 
    # encryption process
    for character in plain_content:
        if character in symbols:
            output += character
        elif character.isupper():
            output += chr((ord(character) + int(encryption_key) - 65) % 26 + 65)
        else:
            output += chr((ord(character) + int(encryption_key) - 97) % 26 + 97)
    
    return output

//Decryption:

    # decryption process
    for character in plain_content:
        if character in symbols:
            output += character
        elif character.isupper():
            output += chr((ord(character) - int(encryption_key) - 65) % 26 + 65)
        else:
            output += chr((ord(character) - int(encryption_key) - 97) % 26 + 97)

//Bruteforcing:

Bruteforcing a Caesar Cipher encryption is easy because there are only 26 possible shift keys.


"A" - index of 1

"Z" - index of 26


Since "Z" is the last letter, there is no index higher than 26. So once "Z" receives a shifted index of 1, it has to recycle back to 26 because there are no other indexes. Therefore, if the key is 36 then the key is also equal to 10.


In order to bruteforce this cipher, all we have to do is decrypt the ciphertext 26 times. Each of those times with a different key ranging from 0 to 26.


The bruteforcing process will allow the user to pick a certain range of keys that they may know the key is in between. This feature is here because users may have a ciphertext that is very large and may take a long time to decrypt the text 26 times.

To implement this feature, all one has to do is incorporate the start and end range of the bruteforce as keyword arguments ranging from 1 to 26 [or 27 due to the non-inclusiveness of Python]. So if the user doesn't specify a range, it will bruteforce using all 26 keys by default.

# bruteforces content
def bruteforce_caesar(plain_content, print_cnt, start_range=0, end_range=27):
    # output variable
    output = ''

    shift_key = start_range
    for shift in range(start_range, end_range):
        output += f'Shift Key: {shift_key}\n'
        shift_key += 1

        for character in plain_content:
            if character in symbols:
                output += character
            elif character.isupper():
                output += chr((ord(character) - shift - 65) % 26 + 65)
            else:
                output += chr((ord(character) - shift - 97) % 26 + 97)

        output += '\n\n'

    # outputs content to cli
    if print_cnt == True:
        print(f'Bruteforced Content:\n{output}\n')

    # outputs content to file
    else:
        with open(print_cnt, 'w') as f:
            f.write(output)
        print('Output written to file sucessfully')

//Implementation:
So when I first started working on this project, I made a crappy, but working, version of the Caesar Cipher in Python, available here. It could encrypt, decrypt, and bruteforce text files using the Caesar Cipher.

I wanted the CodeX to be an effective shell script, one that would work through one-liners and be fast and efficient. Something similar to NMAP or Metasploit, but for cryptography. So it needed to be able to run these processes on both files and inline oneliner text. It needed to be able to output to the terminal or to a file. After three streams worth of implementation, we were finally able to finish implementing our old code, update it, format it, and incorporate it into the CodeX.

All code for The CodeX can be found here.

In the next Cyberlog, we will start figuring out how to incorporate the Vigenere Cipher into the CodeX. Thanks for reading, and as always,

Happy Hacking!


// Socials:

© 2021 by Cosmodium CyberSecurity LLC

236 views0 comments

Recent Posts

See All

Comentários


bottom of page