-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
165 lines (138 loc) · 4.12 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Caesar Cipher</title>
<style>
.width-responsive {
width: 20%;
}
@media screen and (max-width: 992px) {
.width-responsive {
width: 50%;
}
}
@media screen and (max-width: 600px) {
.width-responsive {
width: 85%;
}
}
</style>
</head>
<body>
<div
style="display: flex; flex-direction: column; justify-content: center; align-items: center; margin: auto; margin-top: 20px;">
<div class='width-responsive'>
<div>
Caesar Cipher
<a href="https://en.wikipedia.org/wiki/Caesar_cipher" target='_blank'>(Wikipedia)</a>
</div>
<div style='margin-top: 20px;'>
<div><label for="key">Tipe</label></div>
<div style='margin-top: 5px;'>
<input type="radio" id="encrypt" name="type" value="encrypt">
<label for="encrypt">Enkrip</label>
<input type="radio" id="decrypt" name="type" value="decrypt">
<label for="decrypt">Dekrip</label>
</div>
</div>
<div style="margin-top: 20px">
<div><label for="key">Key</label></div>
<input type="text" id="key" style="width: 100%; margin-top: 5px;" placeholder='Kunci Angka' />
</div>
<div style="margin-top: 20px">
<div><label for="text">Text</label></div>
<textarea name="text" id="text" rows="10" style="width: 100%; margin-top: 5px;"
placeholder='Text untuk di enkripsi/dekripsi'></textarea>
</div>
<div style="margin-top: 20px; display: flex; justify-content: right">
<button onclick="handleGenerate()">Generate!</button>
</div>
<div style="margin-top: 50px">
<div><label for="result">Hasil:</label></div>
<div id="result" style='word-wrap: break-word; margin-top: 10px;'></div>
</div>
</div>
</div>
<script>
const alphabets = [
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
];
function handleGenerate() {
const keyValue = document.getElementById('key').value;
if (!keyValue) return;
const textValue = document.getElementById('text').value;
if (!textValue) return;
const typeValue = document.querySelector('input[name="type"]:checked')?.value || 'encrypt';
const serializedKey = keyValue.replace(/[^0-9]/g, '');
const result = { value: '', key: keyValue, substractIndex: 0 };
for (let i = 0; i < textValue.length; i++) {
const alphabet = textValue[i];
if (alphabet == ' ') {
result.substractIndex += 1
}
if (alphabet == '\n') {
result.value += '<br/>'
result.substractIndex += 1
}
if (alphabet) {
const index = alphabets.findIndex((item) => item == alphabet.toLowerCase());
if (index == -1) {
result.value += alphabet;
} else {
const keys = serializedKey.split('');
const key = Number(keys[(i - result.substractIndex) % keys.length]);
let value = ''
if (typeValue == 'encrypt') {
if (index + key > alphabets.length) {
value = alphabets[(index + key) % alphabets.length];
} else {
value = alphabets[index + key];
}
} else {
if (index - key < 0) {
value = alphabets[(index - key) + alphabets.length];
} else {
value = alphabets[index - key];
}
}
if (alphabet == alphabet.toUpperCase()) {
value = value.toUpperCase()
}
result.value += value;
}
} else {
result.value += alphabet;
}
}
document.getElementById('result').innerHTML = result.value;
}
</script>
</body>
</html>