-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpolyalphabetic_cipher.html
More file actions
156 lines (139 loc) · 4.41 KB
/
Copy pathpolyalphabetic_cipher.html
File metadata and controls
156 lines (139 loc) · 4.41 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Polyalphabetic Cipher</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600&display=swap" rel="stylesheet">
<style>
/* General styling */
body {
font-family: 'Poppins', sans-serif;
margin: 0;
padding: 0;
background: linear-gradient(135deg, #6a11cb, #2575fc);
color: #fff;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
}
h1 {
font-size: 2.5rem;
text-shadow: 2px 2px 10px rgba(0, 0, 0, 0.3);
margin-bottom: 20px;
}
/* Container for input area */
.container {
background: rgba(0, 0, 0, 0.7);
padding: 30px;
border-radius: 15px;
box-shadow: 0 5px 15px rgba(0, 0, 0, 0.4);
width: 100%;
max-width: 600px;
text-align: center;
}
textarea {
width: 100%;
height: 150px;
padding: 15px;
border-radius: 10px;
border: none;
font-size: 1.1rem;
color: #264653;
background-color: #f1faee;
margin-bottom: 20px;
resize: none;
}
input[type="text"] {
padding: 10px;
width: 100%;
font-size: 1rem;
margin-top: 10px;
border-radius: 8px;
border: none;
background-color: #f1faee;
color: #264653;
}
button {
padding: 10px 20px;
background-color: #e9c46a;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 1.1rem;
color: #264653;
transition: background-color 0.3s, transform 0.3s;
}
button:hover {
background-color: #f4a261;
transform: scale(1.05);
}
.result {
margin-top: 20px;
font-size: 1.3rem;
background: #264653;
padding: 15px;
border-radius: 10px;
color: #f1faee;
min-height: 50px;
word-wrap: break-word;
}
</style>
</head>
<body>
<h1>Polyalphabetic Cipher</h1>
<div class="container">
<textarea id="inputText" placeholder="Enter text..."></textarea>
<input type="text" id="keyInput" placeholder="Enter cipher key" value="KEY">
<br>
<button onclick="encryptText()">Encrypt</button>
<button onclick="decryptText()">Decrypt</button>
<div class="result" id="result"></div>
<div class="time" id="timeTaken"></div>
</div>
<script>
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const base = alphabet.length; // 62 characters
function encryptText() {
processText(true);
}
function decryptText() {
processText(false);
}
function processText(isEncrypt) {
let key = document.getElementById("keyInput").value.replace(/[^A-Za-z0-9]/g, "");
let inputText = document.getElementById("inputText").value;
let resultText = "";
let keyIndex = 0;
if (key === "") {
document.getElementById("result").innerText = "Please enter a valid key.";
return;
}
let startTime = performance.now();
for (let i = 0; i < inputText.length; i++) {
let char = inputText[i];
let charPos = alphabet.indexOf(char);
if (charPos === -1) {
// Non-alphanumeric → keep as is
resultText += char;
} else {
let keyChar = key[keyIndex % key.length];
let keyShift = alphabet.indexOf(keyChar);
if (isEncrypt) {
resultText += alphabet[(charPos + keyShift) % base];
} else {
resultText += alphabet[(charPos - keyShift + base) % base];
}
keyIndex++;
}
}
let endTime = performance.now();
let timeTaken = (endTime - startTime).toFixed(4);
document.getElementById("result").innerText = resultText || "Please enter valid text.";
document.getElementById("timeTaken").innerText = "Time taken: " + timeTaken + " ms";
}
</script>
</body>
</html>