-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcipher.html
92 lines (87 loc) · 2.91 KB
/
cipher.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Vigenère Cipher</title>
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
background: #f4f4f4;
padding: 20px;
}
.container {
max-width: 600px;
margin: auto;
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
textarea, input {
width: 100%;
padding: 10px;
margin: 10px 0;
border: 1px solid #ddd;
border-radius: 5px;
}
button {
padding: 10px 15px;
border: none;
background: #007BFF;
color: white;
font-size: 16px;
cursor: pointer;
margin: 5px;
border-radius: 5px;
}
button:hover {
background: #0056b3;
}
</style>
</head>
<body>
<div class="container">
<h2>Vigenère Cipher</h2>
<textarea id="inputText" placeholder="Enter text..." rows="4"></textarea>
<input type="text" id="key" placeholder="Enter key">
<button onclick="encrypt()">Encrypt</button>
<button onclick="decrypt()">Decrypt</button>
<h3>Output:</h3>
<textarea id="outputText" rows="4" readonly></textarea>
</div>
<script>
function vigenereCipher(text, key, encrypt = true) {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
text = text.toUpperCase();
key = key.toUpperCase();
let output = "";
let keyIndex = 0;
for (let i = 0; i < text.length; i++) {
let char = text[i];
let charIndex = alphabet.indexOf(char);
if (charIndex !== -1) {
let shift = alphabet.indexOf(key[keyIndex % key.length]);
let newIndex = encrypt ? (charIndex + shift) % 26 : (charIndex - shift + 26) % 26;
output += alphabet[newIndex];
keyIndex++;
} else {
output += char;
}
}
return output;
}
function encrypt() {
const text = document.getElementById("inputText").value;
const key = document.getElementById("key").value;
document.getElementById("outputText").value = vigenereCipher(text, key, true);
}
function decrypt() {
const text = document.getElementById("inputText").value;
const key = document.getElementById("key").value;
document.getElementById("outputText").value = vigenereCipher(text, key, false);
}
</script>
</body>
</html>