<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>
/*
Untuk panggil sintaks, langsung ketik nama sintaknya
Untuk panggil class, tambahkan titik diawal nama sintaknya
Untuk panggil id, tambahkan hashtag diawal nama sintaknya
*/
body {
background-color: black;
color: lime;
}
.container {
margin: 50px auto;
padding: 20px;
border: 2px solid lime;
max-width: 500px;
background-color: rgb(12, 12, 12);
}
textarea, input {
width: 100%;
background-color: rgb(39, 39, 39);
color: white;
border: 1px solid lime;
padding: 10px;
margin-bottom: 15px;
}
button {
background-color: lime;
cursor: pointer;
padding: 10px;
border: none;
font-weight: bold;
}
.output {
min-height: 50px;
border: 1px solid lime;
background-color: rgb(39, 39, 39);
padding: 15px;
border-radius: 5px;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="container">
<h1>Message Cipher</h1>
<label>Secret Message:</label>
<textarea name="" id="secretMessage" placeholder="Text message here..."></textarea>
<label>Key (Number):</label>
<input type="number" name="" id="key" value="1">
<div class="buttons">
<button onclick="process(true)" id="Encrypt">Encrypt (Forward)</button>
<button onclick="process(false)" id="Decrypt">Decrypt (Reverse)</button>
</div>
<h3>Result:</h3>
<div id="Result" class="output"></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>