Question

Write a Javascript program that implements the Caesar Cipher algorithm. The program must take the message...

Write a Javascript program that implements the Caesar Cipher algorithm. The program must take the message to encrypt and the number of shifting positions, N, as a command line argument, as illustrated in the figure below. Make sure that your program does not crash or give unexpected results when given lower case letters or any other character.

This is what I have so far:

function encrypt(str, N) {

var result =

var Plain = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split('');

for (var i = 0; i < str.length; i ++) {

index = Plain.indexOf(str[i]);

result = Plain[index];

}

return result;

}

Homework Answers

Answer #1

CODE

//decipher the string
let ceaserCipher = () => {
  let str = process.argv[2];
  let N = parseInt(process.argv[3]);
  let decipher = '';
  
  //decipher each letter
  for(let i = 0; i < str.length; i++){
    
    //if letter is uppercase then add uppercase letters
    if(str[i] >= 'A' && str[i] <= 'Z'){
      decipher += String.fromCharCode((str.charCodeAt(i) + N - 65) % 26 + 65);
    }else if(str[i] >= 'a' && str[i] <= 'z'){
      decipher += String.fromCharCode((str.charCodeAt(i) + N - 97) % 26 + 97);
    }
  }
  
  
  return decipher;
}
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
There is a simple encryption scheme that is called “Caesar Cipher”. In a “Caesar Cipher”, the...
There is a simple encryption scheme that is called “Caesar Cipher”. In a “Caesar Cipher”, the letters in a message are replaced by the letters of a “shifted” alphabet. A “Caesar Cipher” encodes a message by shifting each letter in a message by a constant amount of k. If k is 5, A becomes F, B becomes G, C becomes H and so on. Let's use Queue to encode and decode the encrypt message. Set k as -5 and decipher...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...
I am to create three different versions of the following C program code below that implements...
I am to create three different versions of the following C program code below that implements the conversion of unsigned binary numbers into decimal (Base 2 to Base 10 conversion). Version 1: Complete the C program ”bin2dec ver1.c” that implements binary to decimal conversion. The maximum number of binary bits is 32. The program is made of the functions ”unsigned binary to decimal(const char *str)”and ”main”. The parameter ”str” passed to this function points to a C string comprising only...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...