DEV Community

Cover image for 🔐 ROT13 DECRYPTION ALGORITHM IN JAVASCRIPT
Gulcan COSKUN
Gulcan COSKUN

Posted on • Edited on

🔐 ROT13 DECRYPTION ALGORITHM IN JAVASCRIPT

In this tutorial, we will break the ROT13 encryption algorithm and convert it to text.

1. What is encryption?

Encryption is used to translate information into a special code to hide its real meaning [1].

2. What is decryption?

The decryption process is used to decode a hidden message, which is carried out by the message receiver [1].

3. ROT13 Cipher (Rotate By 13 Places)

Image descriptionFigure 3.1 ROT13 cipher

One of the common encryption methods is the ROT13 cipher, which is a simple monoalphabetic cipher with a private key in which letters of the alphabet are shifted by 13 digits [2]. In other words, M becomes Z, A becomes N, etc. 

There are 26 letters in the basic Latin alphabet, so we can say that ROT13 is its own reverse. Therefore, the same process can be used for encoding and decoding [3].

4. Algorithme

4.1 First step

Assume that we have a sentence "TBBQ ZBEAVAT". In this sentence, each letter corresponds to a character code. In the first step, we will convert the letters into character codes.

4.1.1 JavaScript Spring split() Method
First of all, we will write a function in which we will create a new variable called "newString" and we will turn it into an array. In this step, we will use the JavaScript string split() method, which is used to split the given string into an array of strings by separating it into substrings [5]. We can see the result in the console (Figure 4.1) as follows:



function transformToROTI13(stringToTransform) {
  // ⭐️ 1) Create a new variable and convert it to an array

  const newString = stringToTransform.split("");

  return newString;
}

console.log(transformToROTI13("TBBQ ZBEAVAT"));


Enter fullscreen mode Exit fullscreen mode

Image descriptionFigure 4.1 The result of the split() method

4.1.2 JavaScript String charCodeAt() method
This method returns a UTF-16 value (a 16-bit integer between 0 and 65535) which is the Unicode value for a character at a specific position in a string [6]. The place must be between 0 and string.length-1. If the position is out of the range, the charCodeAt() method will return a particular, not-a-number value printed as NaN. We can see the result in the console.



let str = "TBBQ ZBEAVAT";

console.log(str.length); // ⭐️ output 12
console.log(str.charCodeAt(0)); // ⭐️ output 84
console.log(str.charCodeAt(1)); // ⭐️ output 66
console.log(str.charCodeAt(13)); // ⭐️ output NaN


Enter fullscreen mode Exit fullscreen mode

4.1.3 JavaScript Array map() Method
The map() method is used to iterate over an array and use a callback function to modify its elements. Then the callback function is executed on each of the elements of the array [7]. Thus, our code will look like the following:



function transformToROTI13(stringToTransform) {
  // ⭐️ 1) Create a new variable and convert it to an array

  const newString = stringToTransform.split("").map((character) => {
    const code = character.charCodeAt(character);
    console.log(code);
  });

  return newString;
}

transformToROTI13("TBBQ ZBEAVAT");


Enter fullscreen mode Exit fullscreen mode

Image descriptionFigure 4.2 Output of the code in the console

4.1.4 ASCII Table
ASCII, The American Standard Code for Information Interchange is a character encoding standard for text files in computers and other devices. Each symbol in the character set can be represented by a decimal value [8]. In this example, we will use uppercase letters, so the ASCII value of uppercase letters A to Z is between 65 and 90.

Image descriptionFigure 4.3 ASCII Alphabet Characters

4.2 Second step

First of all, we need to make sure that our character code is between 65 and 90. The value in the middle of my list is 78 and that corresponds to the letter N. If the character code is less than 78, we will move forward by 13 characters. If the character code is greater than 78, we will move backward by 13 characters.

For example:



Character code of B is 66, it is less than 78, so we add 13 
result: 66 + 13 = 79

Check it in the Figure 4.3. We can see that 79 corresponds to letter O. 
So we can verify it using the Figure 3.1 which shows that B corresponds to O. 


Enter fullscreen mode Exit fullscreen mode

4.2.1 JavaScript String.fromCharCode() Method
Previously we got integers between 65 and 90 for each letter using the charCodeAt() method. Now we will use the String.fromCharCode() method to return the string constructed from the specified array of UTF-16 code units.



function transformToROTI13(stringToTransform) {
  // ⭐️ 1) Create a new variable and convert it to an array

  const newString = stringToTransform.split("").map((character) => {
    const code = character.charCodeAt(character);
    console.log(code);

    // ⭐️ 2) Check the character code is between 65 and 90
    // Return an unconverted character if it is out of range
    if (code < 65 || code > 90) {
      return String.fromCharCode(code);
      // ⭐️ 3) If the character code is less than 78, move forward 13 characters
    } else if (code < 78) {
      return String.fromCharCode(code + 13);
    }
    // ⭐️ 4) If the character code is less than 78, come back 13 characters
    else {
      return String.fromCharCode(code - 13);
    }
  });

  return newString;
}
transformToROTI13("TBBQ ZBEAVAT");


Enter fullscreen mode Exit fullscreen mode

Image descriptionFigure 4.4 The result of the letters in the console

4.2.2 JavaScript Array join() method
In the final step, we will use the join() method to create and return a new string by concatenating all of the elements in the array. 

Here is our final code, which is easily understandable and readable:



function transformToROTI13(stringToTransform) {
return stringToTransform
.split("")
.map((character) => {
const code = character.charCodeAt(character);

  if (code &lt; 65 || code &gt; 90) {
    return String.fromCharCode(code);
  } else if (code &lt; 78) {
    return String.fromCharCode(code + 13);
  } else {
    return String.fromCharCode(code - 13);
  }
})
.join("");
Enter fullscreen mode Exit fullscreen mode

}

console.log(transformToROTI13("TBBQ ZBEAVAT")); // GOOD MORNING
console.log(transformToROTI13("THYPNA PBFXHA")); // GULCAN COSKUN

Enter fullscreen mode Exit fullscreen mode



  1. Conclusion

In this tutorial, we learned not only how to apply the ROT13 Algorithm but also how to apply some basic JavaScript methods like map(), split(), join(), String.fromCharCode(), and charCodeAt() for decrypting ROT13 Algorithm.

Happy learning …

You can reach out to me through LinkedIn and Medium

6. References

  1. TechTarget
  2. Practical cryptography
  3. Wikipedia
  4. OpenClassrooms online course
  5. GeeksforGeeks
  6. MDN Web Docs
  7. JavaScript Tutorial
  8. ASCII
  9. TechOnTheNet

Top comments (0)