Ceaser Cipher JS Playground
This is a JS playground to generate Ceaser Cipher encryption
by Rajdeep Chandra
HTML
<form onsubmit="ceaserShift()">
<label>Enter your Text</label>
<input type="text" placeholder="Enter your text" id="inputTxt" />
<input type="button" onclick="ceaserShift()" value="Encrypt" />
</form>
<br>
<label>OutPut</label>
<input type="text" placeholder="Enter your text" readonly id="outputTxt" />
JavaScript
const ceaserShift = (str, amount) => {
str = document.getElementById("inputTxt").value;
amount = 90;
if (amount < 0) {
return ceaserShift(str, amount + 26)
}
// we will store the output here
let output = '';
for (let i = 0; i < str.length; i++) {
// holding the value
let ch = str[i];
if (ch.match(/[a-z]/i)) {
// getting the code for that letter
let code = str.charCodeAt(i)
// checking for uppercase letters
if ((code >= 65) && (code <= 90)) {
ch = String.fromCharCode(((code - 65 + amount) % 26) + 65);
}
// checking for lowercase letters
else if ((code >= 97) && (code <= 122)) {
ch = String.fromCharCode(((code - 97 + amount) % 26) + 97);
}
}
// appending the output
output += ch;
}
console.log("output of ceaser cipher" + output)
return output;
}