DEV Community

Cover image for Caesar Cipher in JavaScript: Simple Magic of Encryption with 3 Shifts
Maks Szymczak
Maks Szymczak

Posted on • Edited on

Caesar Cipher in JavaScript: Simple Magic of Encryption with 3 Shifts

I recently discussed the topic of the Caesar Cipher with my mentor, and it greatly intrigued me. Ciphers have always sparked our curiosity. Today, we will explore one of the oldest and simplest - the Caesar Cipher. Named in honor of the famous Roman general Julius Caesar, this shifting cipher will introduce us to the fascinating world of cryptography. We will try to understand if, despite its simplicity, it can be used for secure information transmission. To do this, we will use the JavaScript language to implement and observe in action this classical cipher.

What exactly is the Caesar Cipher?

The Caesar Cipher is a type of substitution cipher in which each letter in the plaintext is replaced by a letter shifted by a constant number of positions in the alphabet. In the case of this cipher, the shift value is known as the encryption key.

Is the Caesar Cipher secure?

Unfortunately, despite its simplicity, the Caesar Cipher does not offer a high level of security. The limited number of possible shifts and the common occurrence of certain letters make maintaining the confidentiality of information challenging.

Let's see how the Caesar Cipher with a shift of 3 positions looks for the text in JavaScript.

Image description

On the console, you will see the Caesar Cipher

Image description

Now let's analyze the code that I worked on with my mentor.

1. Defining the encrypt function:

We are specifying exactly what our function should do. We named it "encrypt" indicating that its purpose will be to encrypt text.

2. Converting text to Unicode codes:

Each character in the text is converted to its Unicode code. The "charCodeAt(0)" function in JavaScript transforms each character in the text into its corresponding Unicode code.

3.Shifting codes by 3 positions up:

We're adding an encryption step that shifts each character in the "textToEncrypt" array by 3 positions up in the alphabet.

4.Converting codes back to characters:

After shifting Unicode codes, we obtain new codes that need to be converted back to characters. The "String.fromCharCode" function in JavaScript performs this conversion, turning codes into their corresponding letters.

5.Displaying the encrypted text:

In the final step, we showcase the encrypted text in the console using "console.log". This allows us to see the outcome of the encryption.

A Simplified Version of the Caesar Cipher Implementation

It's crucial to point out that our implementation of the Caesar Cipher is quite basic. This is not a comprehensive representation of the traditional Caesar Cipher but a significantly simplified version. We've taken this approach for ease of understanding and demonstration purposes, so keep in mind that a full-fledged implementation would involve additional complexities and considerations.

What Can Be Improved?

At the end:
In our exploration of the classic Caesar Cipher, it's important to note that we're not confined to the decimal system. We can just as easily represent encrypted texts in binary or hexadecimal form. This flexibility keeps the Caesar Cipher as an intriguing tool in the realm of information security.

Happy Coding!👨🏻‍💻

Top comments (0)