Caesar / Rot13

Shifting cipher, which was used by Julius Caesar
length:
Key:
length:



This application is also available implemented in Python. Running it as Python downloads a local runtime environment (8MB compressed) into your browser. You can edit the Python code and it will use the values from the upper GUI components. Feel free to try it:
If the Python code below is visible, it is used to calculate the upper output. To use the default (JavaScript) implementation again, just click the hide button. The code below can be edited, so you can experiment with it. You can also download the code and run it in a local terminal on your computer.
Loading local Python runtime environment, please wait ...
Terminal command:
Font size:

Background

The Caesar cipher is named after the Roman military and political leader Gaius Julius Caesar (100 BC – 44 BC). Caesar used this relatively simple form of ciphering to encipher military messages.


Description

The classic version uses the capital letters A-Z, but, in principle, an arbitrary alphabet can be used. The first step is to write the alphabet down two times.

A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z
A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z

Now, the bottom alphabet is shifted by an arbitrary number of positions. The number of positions is the value of the key. Shifting the bottom alphabet 3 positions to the right yields the following result:

A  B  C  D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z
D  E  F  G  H  I  J  K  L  M  N  O  P  Q  R  S  T  U  V  W  X  Y  Z  A  B  C

The letter A becomes the letter D, B is replaced by E, and C replaced by F, etc. The word example would be enciphered to hadpsoh. The upper alphabet is called 'plaintext alphabet' and the lower alphabet is called 'ciphertext alphabet'.


Security

The number of possible keys is identical to the size of the given alphabet. Using the capital letters A-Z as alphabet allows 26 different keys, with the 26th key rendered meaningless because it would map each letter to itself. With only 25 meaningful keys, it would be quite easy to test for all possible keys until the correct one is found (brute-force analysis). The Caesar cipher can also easily be cracked with a frequency analysis.


Internal working of the local Python version

About the code

In this plugin you can control the encryption process in two ways:

  • via the GUI
  • via the command line arguments for the Python program

The Python code is executed purely locally in your browser, without the need to install a Python development environment and also without the Python code being executed somewhere in the cloud. The GUI parameters are passed to the script as command line parameters. Changes in the GUI change the respective command line parameters. The following parameters can be changed:

Grafisches Element Terminal-Parameter Variable im Code
the text to be entered --message text
whether to encrypt or decrypt the entered text --encrypt / --decrypt b_encrypt
the key --key key
the alphabet --alphabet alphabet
whether to keep characters not contained in the alphabet --keep-non-alp b_keep_chars
whether the output text should be shown in blocks of five --blocks-of-five b_block_of_five
Note: For b_keep_chars=True and b_block_of_five=True and b_encrypt=True, spaces are removed even if they are not in the alphabet so there are no spaces inside the blocks of 5.

The code is a simple implementation of the Caesar cipher in Python. Each input character is converted to upper case and then processed. During the encryption, the Latin alphabet is used. For each character in the encrypted text, it is determined whether the character can be found in the given alphabet. If the character is found in the alphabet, the new character is determined by using the key. In case an input character is not included in the alphabet, the character is taken over unchanged (for example special characters). If a check mark is placed in the 'Blocks of 5' checkbox the output is displayed in blocks of five.


About the editor and the Python runtime

The editor available on this page was implemented with CodeMirror[1], which is a versatile text editor for the Browser and is implemented in JavaScript. CodeMirror has a number of add-ons for different programming languages. Pyodide[2] is used to execute the Python code.

Pyodide brings the Python runtime environment via WebAssembly into the browser, together with NumPy, Pandas, Matplotlib, parts of SciPy and NetworkX. In the editor there is now an interpreter for Python 3.x. You can see which exact Python version is used by Pyodide – by adding the following statement to the code: print(sys.version)


References

[1] Codemirror: https://codemirror.net/