<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>
body {
background-color: black;
color: lime;
}
.container {
margin: 50px auto;
padding: 20px;
border: 2px solid;
max-width: 500px;
}
textarea, input {
width: 100%;
background-color: black;
border: 2px solid green;
padding: 10px;
margin-bottom: 15px;
color: white;
}
button {
background-color: lime;
/* font-size: 20px; */
cursor: pointer;
padding: 10px;
border: none;
font-weight: bold;
}
.output {
background-color: rgb(31, 31, 31);
min-height: 100px;
border: 2px solid lime;
border-radius: 5px;
color: lime;
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="Enter Text here"></textarea>
<label>Key (number): </label>
<input type="number" name="" id="key" value="1">
<div class="buttons">
<button onclick="process(true)">Encrypt</button>
<button onclick="process(false)">Decrypt</button>
</div>
<h3>Result:</h3>
<div class="output" id="result">
</div>
</div>
<script>
function process(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
)
} else if (charcode >= 97 && charcode <= 122) {
Result += String.fromCharCode(
((charcode - 97 + key) % 26 + 26) % 26 + 97
)
} else {
Result +=Text[index];
}
}
document.getElementById('result').innerText = Result;
};
</script>
</body>
</html>