Repository navigation
Expand file tree
/
Copy pathapp.js
More file actions
239 lines (209 loc) · 6.94 KB
/
Copy pathapp.js
File metadata and controls
239 lines (209 loc) · 6.94 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
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
const state = { ids: [], ic: null, revIC: null, encCache: {} };
async function loadV() {
const r = await fetch("v.json");
if (!r.ok) throw new Error("Не найден v.json");
return r.json();
}
async function loadIC() {
const r = await fetch("ic.json");
if (!r.ok) throw new Error("Не найден ic.json");
return r.json();
}
async function loadEnc(id) {
if (state.encCache[id]) return state.encCache[id];
const r = await fetch(id + ".json");
if (!r.ok) throw new Error("Не найден " + id + ".json");
const j = await r.json();
state.encCache[id] = j;
return j;
}
function buildRevIC(ic) {
const rev = {};
for (const [sym, num] of Object.entries(ic)) rev[num] = sym;
return rev;
}
function buildReverse(enc) {
const rev = {};
for (const [k, code] of Object.entries(enc)) {
if (k === "id") continue;
rev[code] = k;
}
return rev;
}
function encode(text, enc) {
const sep = enc["_"];
const line = enc["#"];
const lines = text.split(/\r?\n/);
const parts = [];
const allCodes = [];
lines.forEach((ln, li) => {
const lineParts = [];
for (const ch of ln) {
const num = state.ic[ch];
if (num === undefined) throw new Error(`Символ "${ch}" отсутствует в ic.json`);
const numCodes = [];
for (const d of String(num)) {
const code = enc[d];
if (code === undefined) throw new Error(`Цифра "${d}" отсутствует в кодировке "${enc.id}"`);
numCodes.push(code);
allCodes.push(code);
}
lineParts.push(numCodes.join(""));
}
parts.push(lineParts.join(sep));
if (li < lines.length - 1) parts.push(line);
});
const encoded = parts.join("");
let key = "";
if (allCodes.length > 0) {
const first = allCodes[0];
const third = allCodes[2] !== undefined ? allCodes[2] : allCodes[allCodes.length - 1];
const last = allCodes[allCodes.length - 1];
key = first.slice(-1) + third.slice(-1) + last.slice(-1);
}
return { encoded, key };
}
function decode(bits, enc) {
const rev = buildReverse(enc);
const sep = enc["_"];
const line = enc["#"];
const sample = Object.entries(enc).find(([k]) => k !== "id")[1];
const LEN = sample.length;
const chunks = [];
for (let i = 0; i < bits.length; i += LEN) chunks.push(bits.slice(i, i + LEN));
let out = "";
let numBuf = "";
function flush() {
if (!numBuf) return;
const sym = state.revIC[numBuf];
if (sym === undefined) throw new Error("В ic.json нет номера " + numBuf);
out += sym;
numBuf = "";
}
for (const c of chunks) {
if (c === sep) { flush(); continue; }
if (c === line) { flush(); out += "\n"; continue; }
const d = rev[c];
if (d === undefined) throw new Error("Неизвестный код: " + c);
numBuf += d;
}
flush();
return out;
}
function keyFromBits(bits, enc) {
const sep = enc["_"];
const line = enc["#"];
const sample = Object.entries(enc).find(([k]) => k !== "id")[1];
const LEN = sample.length;
const chunks = [];
for (let i = 0; i < bits.length; i += LEN) chunks.push(bits.slice(i, i + LEN));
const groups = chunks.filter((c) => c !== sep && c !== line);
let key = "";
if (groups.length > 0) {
const first = groups[0];
const third = groups[2] !== undefined ? groups[2] : groups[groups.length - 1];
const last = groups[groups.length - 1];
key = first.slice(-1) + third.slice(-1) + last.slice(-1);
}
return { key };
}
function randomId(len = 8) {
const abc = "abcdefghijklmnopqrstuvwxyz0123456789";
let s = "";
for (let i = 0; i < len; i++) s += abc[Math.floor(Math.random() * abc.length)];
return s;
}
function download(filename, content) {
const blob = new Blob([content], { type: "text/plain" });
const a = document.createElement("a");
a.href = URL.createObjectURL(blob);
a.download = filename;
a.click();
URL.revokeObjectURL(a.href);
}
function render(root) {
root.innerHTML = `
<style>
.ok { color: green; }
.err { color: red; }
</style>
<h3>CatCode</h3>
<div><label>Тип кодировки: <select id="encSel"></select></label></div>
<h4>Зашифровать</h4>
<textarea id="plain" rows="5" cols="50" placeholder="Текст..."></textarea><br>
<button id="btnEnc">Зашифровать → .catcode</button>
<div id="encMsg"></div>
<hr>
<h4>Расшифровать</h4>
<input type="file" id="fileIn" accept=".catcode">
<div><label>Ключ: <input id="keyIn" type="text" maxlength="3" size="5"></label></div>
<div><label>Тип кодировки: <select id="encSel2"></select></label></div>
<button id="btnDec">Расшифровать</button>
<pre id="decOut"></pre>
<div id="decMsg"></div>
`;
const fill = (sel) => {
sel.innerHTML = "";
for (const id of state.ids) {
const o = document.createElement("option");
o.value = id;
o.textContent = id;
sel.appendChild(o);
}
};
fill(root.querySelector("#encSel"));
fill(root.querySelector("#encSel2"));
root.querySelector("#btnEnc").onclick = async () => {
const encId = root.querySelector("#encSel").value;
const text = root.querySelector("#plain").value;
const msg = root.querySelector("#encMsg");
msg.textContent = "";
try {
const enc = await loadEnc(encId);
const { encoded, key } = encode(text, enc);
const fname = "catcode_" + randomId() + ".catcode";
download(fname, encoded);
msg.innerHTML = '<span class="ok">Файл: <b>' + fname + '</b><br>Ключ: <b>' + key + '</b></span>';
} catch (e) {
msg.innerHTML = '<span class="err">Ошибка: ' + e.message + '</span>';
}
};
root.querySelector("#btnDec").onclick = async () => {
const encId = root.querySelector("#encSel2").value;
const keyIn = root.querySelector("#keyIn").value.trim();
const fileEl = root.querySelector("#fileIn");
const out = root.querySelector("#decOut");
const msg = root.querySelector("#decMsg");
out.textContent = "";
msg.textContent = "";
if (!fileEl.files[0]) {
msg.innerHTML = '<span class="err">Выбери файл .catcode</span>';
return;
}
try {
const bits = (await fileEl.files[0].text()).trim();
const enc = await loadEnc(encId);
const { key: realKey } = keyFromBits(bits, enc);
if (keyIn !== realKey) {
msg.innerHTML = '<span class="err">Неверный ключ.</span>';
return;
}
out.textContent = decode(bits, enc);
msg.innerHTML = '<span class="ok">OK</span>';
} catch (e) {
msg.innerHTML = '<span class="err">Ошибка: ' + e.message + '</span>';
}
};
}
(async function main() {
const root = document.getElementById("root");
try {
state.ic = await loadIC();
state.revIC = buildRevIC(state.ic);
state.ids = await loadV();
} catch (e) {
root.innerHTML = '<span class="err">Ошибка загрузки: ' + e.message + '</span>';
return;
}
render(root);
})();