# Encrypting a string JavaScript

In this problem statement, our task is to encrypt the input plain text in ciphertext with the help of Javascript functionalities. There are several methods that can be used to encrypt a message in Javascript. One simple method to encrypt the string is Caesar Cipher.

## Understanding the problem statement

The problem statement is to write a function in Javascript that will help to encrypt the given input string into a non readable format. For example, if we have a string "hello world", the encrypted version of this string is "khoor zruog" by shifting each character by two positions.

## What is Caesar Cipher Algorithm ?

The Caesar Cipher technique is basically used to encrypt the messages. It is the method used to shift the letter in the plaintext by shifting the position of the letters to convert it into ciphertext. For example if the shifting number is 3, the letter 'A' will be encrypted as 'D', the letter 'B' will become 'E' and so on. The same shifting number will be applied to all the letters present in the message.

So in simple terms we can say that the Caesar Cipher is a type of substitution cipher in which each letter in the plaintext is replaced by another letter as per the specific rule. But this is very simple encryption which can be broken easily with the help of brute-force algorithm in which an attacker tries to check all the possible key values of letters in the ciphertext to know the original message.

The technique has this drawback but still this method is sometimes used in some situations in which security is not a big concern.

## Logic for the given problem

For the code we will create a function to perform the encryption. Inside this function we will pass two parameters: first is the string to encrypt and second the number of positions we want to shift each character in the string. Then we will loop to iterate all the characters in the string and check if the character is in uppercase or lowercase so apply shift as per the case. Now we will shift each character as per the given key and show the encrypted message as the output.

## Algorithm

Step 1 − Declare a function called encryptMsg which is using two parameters str and shift.

Step 2 − Define an empty string variable to store the encrypted message.

Step 3 − Loop through each character in the string and check if the character is in uppercase, lowercase or in another format.

Step 4 − And after we will get character code for each character and store it in charCode variable.

Step 5 − As we know, the character code or ASCII code for capital letters are from 65 to 90 and for lowercase characters are from 97 to 122. So check these characters as per the numbers given and apply shift to convert the character in cipher text.

Step 6 − Return the result in encrypted format.

## Code for the algorithm

// function for encryption the input message
function encryptMsg(str, shift) {
let encreptedStr = ' ';

for (let i = 0; i < str.length; i++) {
let charCode = str.charCodeAt(i);

if (charCode >= 65 && charCode <= 90) {
// uppercase letters
encreptedStr += String.fromCharCode((charCode - 65 + shift) % 26 + 65);
} else if (charCode >= 97 && charCode <= 122) {
// lowercase letters
encreptedStr += String.fromCharCode((charCode - 97 + shift) % 26 + 97);
} else {
// non-alphabetic characters
encreptedStr += str.charAt(i);
}
}
return encreptedStr;
}
const plaintext = 'Hello Tutorials Point';
const shift = 5;
const ciphertext = encryptMsg(plaintext, shift);
console.log("The plain text has been converted in cipher text ---->", ciphertext);


## Complexity

The time taken by the function is O(n) because the method uses a loop to iterate over each character in the message given. And n is the size of the given string. And the space used by the code is also O(n) as it is storing the result as only the encrypted version of the string.

## Conclusion

So the above created function can be used to encrypt the message with the time complexity O(n). We have basically used ASCII code and shift number to convert the given message in ciphertext. And also used a built-in method of Javascript called fromCharCode to solve the given problem.

Updated on: 18-May-2023

3K+ Views