-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
59 lines (45 loc) · 3.1 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
<!DOCTYPE html>
<html>
<head>
<title>優雅な紅茶のひと時を</title>
</head>
<body>
<h1>文字列の暗号化・復号化</h1>
<label for="input">暗号化したい文字列:</label>
<input type="text" id="input" />
<label for="key">合言葉:</label>
<input type="text" id="key" />
<button onclick="encrypt()">暗号化</button>
<button onclick="decrypt()">復号化</button>
<h2>暗号化・復号化された文字列:</h2>
<p id="result"></p>
<script>
function encrypt() {
var input = document.getElementById("input").value;
var key = document.getElementById("key").value;
var encrypted = "";
for (var i = 0; i < input.length; i++) {
var charCode = input.charCodeAt(i);
var keyIndex = i % key.length;
var keyChar = key.charCodeAt(keyIndex);
var encryptedCharCode = charCode + keyChar;
encrypted += String.fromCharCode(encryptedCharCode);
}
document.getElementById("result").textContent = encrypted;
}
function decrypt() {
var encrypted = document.getElementById("result").textContent;
var key = document.getElementById("key").value;
var decrypted = "";
for (var i = 0; i < encrypted.length; i++) {
var charCode = encrypted.charCodeAt(i);
var keyIndex = i % key.length;
var keyChar = key.charCodeAt(keyIndex);
var decryptedCharCode = charCode - keyChar;
decrypted += String.fromCharCode(decryptedCharCode);
}
document.getElementById("result").textContent = decrypted;
}
</script>
</body>
</html>