<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Secret Message Chiper</title>
<style>
/*
CSS
Untuk sintaks html, bisa langsung tulis seperti body {}
Untuk class html, bisa tulis titik seperti .container {}
Untuk id html, bisa tulis pagar seperti #InputMessage {}
*/
body {
background-color: rgb(56, 56, 56);
color: aqua;
}
.container {
max-width: 500px;
border: 2px solid;
margin: 50px auto;
padding: 20px;
}
textarea, input {
width: 100%;
background-color: grey;
color: white;
border: 1px solid;
padding: 10px;
margin-bottom: 15px;
}
button {
background-color: aqua;
cursor: pointer;
padding: 10px;
border: none;
font-weight: bold;
}
.output {
min-height: 50px;
background-color: grey;
padding: 15px;
border-radius: 5px;
word-wrap: break-word;
}
</style>
</head>
<body>
<div class="container">
<h1>Secret Message Chiper</h1>
<label for="InputMessage">Secret Message:</label>
<textarea name="" id="InputMessage"></textarea>
<label for="KeyNumber">Key (Number)</label>
<input type="number" name="" id="KeyNumber">
<button onclick="process(true)">Enkription</button>
<button onclick="process(false)">Dekription</button>
<h3>Result:</h3>
<div id="Result" class="output"></div>
</div>
<script>
function process(isEncrypt) {
const text = document.getElementById('InputMessage').value;
let key = parseInt (document.getElementById('KeyNumber').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 - 65 + key) % 26 + 26) % 26 + 97
);
} else {
result += text[index];
}
}
document.getElementById('Result').innerText = result;
};
</script>
</body>