In: Computer Science
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.
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.