Difference between revisions of "ISCL Lab3"
Jump to navigation
Jump to search
(Created page with "<pre> <html> <head> <script> function encrypt(text, s) { let result=""; for (let i = 0; i < text.length; i++) { let char = text[i];...") |
|||
| Line 1: | Line 1: | ||
| + | '''Perform encryption and Decryption of Caesar cipher. Write a script for performing these operations''' | ||
<pre> | <pre> | ||
<html> | <html> | ||
| Line 9: | Line 10: | ||
{ | { | ||
let char = text[i]; | let char = text[i]; | ||
| − | + | let ch = String.fromCharCode(char.charCodeAt() + s); | |
result += ch; | result += ch; | ||
} | } | ||
Latest revision as of 23:50, 29 February 2024
Perform encryption and Decryption of Caesar cipher. Write a script for performing these operations
<html>
<head>
<script>
function encrypt(text, s)
{
let result="";
for (let i = 0; i < text.length; i++)
{
let char = text[i];
let ch = String.fromCharCode(char.charCodeAt() + s);
result += ch;
}
return result;
}
function dencrypt(text, s)
{
let result=""
for (let i = 0; i < text.length; i++)
{
let char = text[i];
let ch = String.fromCharCode(char.charCodeAt() -s);
result += ch;
}
return result;
}
// Driver code
let text = "HORRIBLE";
let s = 4;
document.write("Text : " + text + "<br>");
document.write("Shift : " + s + "<br>");
document.write("Cipher: " + encrypt(text, s) + "<br>");
let text1 = encrypt(text, s);
document.write("Text1 : " + text1 + "<br>");
document.write("Shift : " + s + "<br>");
document.write("Cipher: " + dencrypt(text1, s) + "<br>");
</script>
</head>
</html>