-
Notifications
You must be signed in to change notification settings - Fork 0
/
ricrypt.html
244 lines (231 loc) · 6.91 KB
/
ricrypt.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<!DOCTYPE html>
<html>
<head>
<title>ricrypt</title>
<style>
h1 {
font-family: monospace;
}
body {
background-color: #f5f5f5;
}
.container {
width: 80%;
margin: 0 auto;
text-align: center;
}
#text {
margin: 20px 0;
}
input {
font-family: monospace;
padding: 10px;
margin: 5px;
width: 90%;
border: 1px solid #ccc;
border-radius: 5px;
text-align: center;
}
textarea {
font-family: monospace;
padding: 10px;
margin: 5px;
width: 90%;
border: 1px solid #ccc;
border-radius: 5px;
resize: none;
height: auto;
text-align: center;
}
#buttons {
margin: 20px 0;
}
button:hover {
filter: brightness(95%);
}
button:active {
filter: brightness(85%);
}
button {
font-family: monospace;
padding: 10px;
margin: 5px;
border-radius: 5px;
border: 1px solid #ccc;
width: 45.3%;
cursor: pointer;
}
.blue {
background-color: #269dd9;
color: white;
}
.red {
background-color: rgb(214, 66, 56);
color: white;
}
.green {
background-color: rgb(43, 183, 43);
color: white;
}
.purple {
background-color: rgb(186, 52, 175);
color: white;
}
#copy-button {
width: 30%;
margin-top: 15px;
}
#download-button {
width: 30%;
margin-top: 15px;
}
#output {
padding: 10px;
margin: 5px;
width: 90%;
}
#outputArea {
margin: 20px 0;
}
</style>
<script src="ricrypt.js" type="text/javascript"></script>
<script>
window.acK = "";
let keyTimeout;
function adjustTextareaHeight(textarea) {
textarea.style.height = "auto";
textarea.style.height = textarea.scrollHeight + "px";
}
function handleKeyInput(event) {
const input = event.target;
const inputValue = input.value;
// Prevent further propagation
event.stopPropagation();
// Clear the existing timeout if any
if (keyTimeout) {
clearTimeout(keyTimeout);
}
// Detect if the user pressed the delete key
if (event.inputType === 'deleteContentBackward') {
window.acK = window.acK.slice(0, -1);
input.value = "*".repeat(inputValue.length);
return;
}
// Get the last character typed
const lastChar = inputValue.charAt(inputValue.length - 1);
// Update the actual key
window.acK += lastChar;
// Replace the input value with asterisks except for the last character
input.value = "*".repeat(inputValue.length - 1) + lastChar;
// Set a timeout to replace the last character with an asterisk after 2 seconds
keyTimeout = setTimeout(() => {
input.value = "*".repeat(inputValue.length);
}, 2000);
}
function copyOutput() {
const output = document.querySelector("textarea[name='output']");
let text = output.value;
if (text === "") {
text = "No output";
}
navigator.clipboard.writeText(text);
}
function downloadOutput() {
const output = document.querySelector("textarea[name='output']");
let text = output.value;
if (text === "") {
text = "No output";
}
const blob = new Blob([text], { type: "text/plain" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = "encrypted.txt";
a.click();
}
function handleFileUpload(event) {
const file = event.target.files[0];
if (!file) {
alert("No file selected.");
return;
}
const reader = new FileReader();
reader.onload = function (e) {
const content = e.target.result;
// if the content contains non ASCII characters, alert the user
if (!/^[\x00-\x7F]*$/.test(content)) {
alert("File contains non-ASCII characters.");
return;
}
const inputText = document.getElementById("inputText");
inputText.value = content;
adjustTextareaHeight(inputText);
};
reader.onerror = function () {
alert("Error reading file.");
};
reader.readAsText(file, "UTF-8");
}
</script>
</head>
<body>
<div class="container">
<h1>ricrypt</h1>
<!-- text input box -->
<div id="text">
<textarea
type="text"
name="text"
id="inputText"
placeholder="Input text"
spellcheck="false"
oninput="adjustTextareaHeight(this)"
></textarea>
<!-- key input box -->
<input
type="text"
name="key"
id="keyInput"
placeholder="Encryption key"
maxlength="10"
spellcheck="false"
oninput="handleKeyInput(event)"
/>
</div>
<!-- file picker -->
<input
type="file"
id="fileInput"
accept=".txt"
onchange="handleFileUpload(event)"
/>
<!-- encrypt button -->
<div id="buttons">
<button id="encrypt-button" class="blue">Encrypt</button>
<!-- decrypt button -->
<button id="decrypt-button" class="red">Decrypt</button>
</div>
<!-- output box -->
<div id="outputArea">
<textarea
type="text"
name="output"
id="output"
placeholder="Output"
spellcheck="false"
disabled
></textarea>
<button id="copy-button" class="green" onclick="copyOutput()">
Copy
</button>
<button
id="download-button"
class="purple"
onclick="downloadOutput()"
>
Download
</button>
</div>
</div>
</body>
</html>