Question

In: Computer Science

Write a program in javascript to encrypt and decrypt the user input using the caesar algorithm...

Write a program in javascript to encrypt and decrypt the user input using the caesar algorithm with the substitution algorithm. Specify the min and max of the message user can enter to encrypt. Specify the length of the key user can enter to encrypt and decrypt the message. document your design by words or diagrams.

Solutions

Expert Solution

Dear student I understand you need help with the caeser cipher algo in cryptography.Dont worry we will follow the procedure and answer your question as follows.

The Caesar Cipher strategy is one of the most punctual and easiest strategy for encryption procedure. It's just a kind of replacement figure, i.e., each letter of a given book is supplanted by a letter some fixed number of positions down the letter set. For instance with a move of 1, A would be supplanted by B, B would become C, etc. The strategy is evidently named after Julius Caesar, who obviously utilized it to speak with his authorities.

Along these lines to encode a given book we need a whole number worth, known as move which shows the quantity of position each letter of the content has been descended.

The encryption can be spoken to utilizing secluded number-crunching by first changing the letters into numbers, as indicated by the plan, A = 0, B = 1,… , Z = 25. Encryption of a letter by a move n can be portrayed numerically as.


This Encryption Phase with shift n


This is Decryption Phase with shift n

Consider the following given code written in javascript:-

/*
"Encrypt" like this:
caesarShift('Attack at dawn!', 12);
And "decrypt" like this:
caesarShift('Mffmow mf pmiz!', -12); // Returns "Attack at dawn!"
For simplicity, only works with ASCII characters.
* * * * * * * * * * * *
var caesarShift = function (str, amount) {
// Wrap the amount
if (amount < 0) {
return caesarShift(str, amount + 26);
}
// Make an output variable
var output = "";
// Go through each character
for (var i = 0; i < str.length; i++) {
// Get the character we'll be appending
var c = str[i];
// If it's a letter...
if (c.match(/[a-z]/i)) {
// Get its code
var code = str.charCodeAt(i);
// Uppercase letters
if (code >= 65 && code <= 90) {
c = String.fromCharCode(((code - 65 + amount) % 26) + 65);
}
// Lowercase letters
else if (code >= 97 && code <= 122) {
c = String.fromCharCode(((code - 97 + amount) % 26) + 97);
}
}
// Append
output += c;
}
// All done!
return output;
};

You want to move it to the left by 65 so A is no code 65 but 0 so you have
0 A
1 B
2 C ...
so then you can easily go to the begining of alphabet when you go past 25th letter which has code 90 - Z (when moved to the left by 65 it's code is 25)
But then at the end you have to add again this 65 so you have your letter code and not the position in alphabet.

I think I have given my max effort to answer your question...I hope You have understood the following program and the explanation in detail about what exactly is caeser cipher algo is.


Related Solutions

how to write program in java for encrypt and decrypt input text using DH algorithm
how to write program in java for encrypt and decrypt input text using DH algorithm
Write a small program to encrypt and decrypt a message using Python library.
Write a small program to encrypt and decrypt a message using Python library.
Using Python, write a simple application that takes user input of plaintext and key, and encrypt...
Using Python, write a simple application that takes user input of plaintext and key, and encrypt the plaintext with Vigenere Cipher. The application should then print out the plaintext and the ciphertext.
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using...
Write a Java program that uses the RC4 cipher algorithm to encrypt the following message using the word CODES as the key:   Cryptography is a method of protecting information and communications through the use of codes so that only those for whom the information is intended can read and process it. Instead of using stream length 256, we will use length 26. When encrypting, let A = 0 to Z = 25. Ignore spaces and punctuations and put the message...
Part 1: Design a Cipher allow user to use a “key” to encrypt and decrypt the...
Part 1: Design a Cipher allow user to use a “key” to encrypt and decrypt the message. Use at least two ciphers combined to design your own cipher Specify the min. and max. length of the message user can enter to encrypt Specify the length of the “key” user can enter to encrypt and decrypt the message Part 2: Programme the cipher and make it available to validate. Cleartext for the original programming scripts has to submitted. A frontend webpage...
Write Java program that asks a user to input a letter, converts the user input to...
Write Java program that asks a user to input a letter, converts the user input to uppercase if the user types the letter in lowercase, and based on the letter the user the user enters, display a message showing the number that matches the letter entered. For letters A or B or C display 2 For letter D or E or F display 3 For letter G or H or I display 4 For letter J or K or L...
Write in javaScript: User input 5 words in one input, and print out the longest word.
Write in javaScript: User input 5 words in one input, and print out the longest word.
write a java program to Translate or Encrypt the given string : (input char is all...
write a java program to Translate or Encrypt the given string : (input char is all in capital letters) { 15 } *) Each character replaced by new character based on its position value in english alphabet. As A is position is 1, and Z is position 26. *) New characters will be formed after skipping the N (position value MOD 10) char forward. A->A+1= B , B->B+2=D ,C->C+3=F, .... Y->Y+(25%10)->Y+5=D A B C D E F G H I...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Exercise 4 – Lists and input Using a function Write a program that prompts the user...
Exercise 4 – Lists and input Using a function Write a program that prompts the user to input a sequence of words. The program then displays a list of unique words (words that only occurred once, i.e. no repeated).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT