<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The Secret Message Cipher</title>
<style>
/*To call syntax, */
body {
background-color: navy;
color: white;
font-family: 'Courier New', Courier, monospace;
}
.container {
margin: 50px auto;
padding: 20px;
border: 2px solid;
max-width: 500px;
}
textarea, input {
width: 100%;
background-color: navy;
color: white;
border: 2px solid;
padding: 10px;
margin-bottom: 15px;
}
button {
background-color: rgb(3, 3, 97);
cursor: pointer;
padding: 10px;
border: none;
font-weight: bold;
color: white;
font-family: 'Courier New', Courier, monospace;
}
.output {
background-color: midnightblue;
min-height: 100px;
border: 2px solid;
border-radius: 5px;
color: white;
padding: 15px;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="container">
<h1>The Secret Message Cipher</h1>
<label>Secret Message:</label>
<textarea name="" id="secretmessage" placeholder="Type message here..."></textarea>
<label>Key (Number):</label>
<input type="number" name="" id="key" value="1">
<div class="buttons">
<button onclick="proccess(true)">Encryption (hide message)</button>
<button onclick="proccess(false)">Decryption (show message)</button>
</div>
<h3>Result:</h3>
<div class="output" id="result"></div>
</div>
<script>
function proccess(IsEncrypt) {
const text = document.getElementById('secretmessage').value;
let key = parseInt(document.getElementById('key').value);
let result = "";
if (!IsEncrypt) {
key = -key;
}
for (let index = 0; index < text.length; index++) {
const element = text[index];
let charCode = text.charCodeAt(index);
if (charCode >= 65 && charCode <= 90) {
result += String.fromCharCode(
((charCode - 65 + key) % 26 + 26) % 26 + 65
//A = 65 => ((65 - 65 + 1) % 26 + 26) % 26 + 26
)
} else if (charCode >= 97 && charCode <= 122) {
result += String.fromCharCode(
((charCode - 97 + key) % 26 + 26) % 26 + 97
//A = 65 => ((65 - 65 + 1) % 26 + 26) % 26 + 26
)
} else {
result += text[index];
}
}
document.getElementById('result').innerText = result;
};
</script>
</body>
</html>