April 2021
The Caesar cipher is one of the oldest and simplest ciphers known to man. It works by taking a key, that both the sender and the recipient know, and shifting each letter in your message by that key (a substitution cipher).
So, as an example, say we have the word github
as our plaintext, and our key is 12. The resulting
cipher-text would be suftgn
.
Let's break that down step-by-step:
g
.s
.Keep in mind that as a substitution cipher, and especially as a monoalphabetic substitution cipher, the encrypted message is vulnerable to cryptanalysis as well as frequency analysis, as the frequency of letters in the text doesn't change, it is just shifted by the key.
The program I wrote takes a string for the message, as well as an integer the key. It then outputs an encrypted message by converting each letter to its ASCII value, adding the key, then converting back, all the while correcting for mistakes (A 'mistake' in this case would be an ASCII value over 122, which is z. In this case, the program finds the difference between the given encrypted ASCII value and 122, and just starts again from the beginning of the alphabet). For simplicity's sake, the program skips (and therefore keeps) punctuation and spaces as well as makes/outputs all letters in lowercase.
Let's break that down as well, as it's a bit more of an involved process:
The decryption function is simply a brute force decryption, as the number of possibilities is relatively small. The program cycles through every possible key (1 - 26) and outputs what might be the plaintext for each related key.
It could be interesting to use the actual encryption function as the decryption function by giving it the encrypted text and cycling through all possible keys. (This is kinda what the decryption function is already doing but within its own function)
More code can be found on this project's Github repo
-js