-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColumnar Transposition Solver.js
More file actions
140 lines (130 loc) · 3.92 KB
/
Columnar Transposition Solver.js
File metadata and controls
140 lines (130 loc) · 3.92 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
// Once again, "Trigrams.js" is used to evaluate permutations.
const alpha = Array.from("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
let solutions = [[[""]], [], []];
let best = 0;
function permutations(lim, n=0, used=[]) {
for (let v of choices) {
while (used.length !== n) {
used.pop();
}
if (!used.includes(v)) {
used.push(v);
if (n + 1 === lim) {
perms.push([...used]);
} else {
permutations(lim, n + 1, used);
}
}
}
}
function fitness(arr) {
let total = 0;
for (let i = 0; i < arr.length - 2; i++) {
total += trigrams[arr.slice(i, i + 3).join("")];
}
return total;
}
function step(key, columns) {
const plaintext = [];
for (let i = 0; i < block; i++) {
for (let j of key) {
plaintext.push(columns[Number(j)][i]);
}
}
return plaintext;
}
function solve() {
solutions = [[], [], []];
choices = Array.from({ length: keyLen }, (_, i) => i);
perms = [];
permutations(keyLen);
for (let key of perms) {
let plaintext = step(key, columns);
solutions[0].push(plaintext);
solutions[1].push(fitness(plaintext));
solutions[2].push(key);
}
best = solutions[1].indexOf(Math.max(...solutions[1]))
buttons.key.value(solutions[2][best]);
solution = step(buttons.key.value().split(","), columns).join("");
}
function drawUI() {
fill(200);
stroke(200);
rect(windowWidth*(3/4), 0, windowWidth/4, windowHeight);
fill(50, 100);
rect(windowWidth/100, windowHeight/50+20, windowWidth*(72/100), windowHeight*(46/50)-20, 10);
stroke(0, 0);
fill(200);
text(solution, windowWidth/100+5, windowHeight/50+25, windowWidth*0.72-10, windowHeight*0.92-5);
fill(0);
textSize(windowHeight/13.8);
text("Text Size:", windowWidth*(76/100), windowHeight*(1.75/6))
text("Key Len:", windowWidth*(76/100), windowHeight*(2.75/6))
}
function copyPlaintext() {
let decrypt = document.createElement("textarea");
document.body.appendChild(decrypt);
decrypt.value = solution;
decrypt.select();
document.execCommand("copy");
decrypt.remove();
}
function keyPressed() {
switch (keyCode) {
case 32: solve(); break;
case ENTER: copyPlaintext(); break;
}
}
function setup() {
textWrap(CHAR);
solution = "";
buttons = {
input: createInput(),
key: createInput("0,1,2,3,4"),
size: createInput("20"),
len: createInput("5"),
copy: createButton("Copy"),
auto: createButton("Auto-Solve")
}
buttons.auto.mousePressed(solve);
buttons.copy.mousePressed(copyPlaintext);
}
function draw() {
positions = {
input: [windowWidth/100, windowHeight/100],
key: [windowWidth*(77/100), windowHeight/25],
size: [windowWidth - windowHeight*(1/5), windowHeight*(1.25/6)],
len: [windowWidth - windowHeight*(1/5), windowHeight*(2.25/6)],
copy: [windowWidth*(77/100), windowHeight*(4/6)],
auto: [windowWidth*(77/100), windowHeight*(5/6)]
}
sizes = {
input: [windowWidth*(73/100), 20],
key: [windowWidth*(21/100), windowHeight*(1/7)],
size: [windowHeight*(1/7), windowHeight*(1/7)],
len: [windowHeight*(1/7), windowHeight*(1/7)],
copy: [windowWidth*(21/100), windowHeight*(1/7)],
auto: [windowWidth*(21/100), windowHeight*(1/7)]
}
for (let v in buttons) {
buttons[v].position(positions[v][0], positions[v][1]);
buttons[v].size(sizes[v][0], sizes[v][1]);
if (v != "input") {
buttons[v].style("font-size", `${windowHeight/13.8}px`);
buttons[v].style("border-radius", "10px");
}
}
createCanvas(windowWidth-20, windowHeight-20);
background(100);
textSize(Number(buttons.size.value()));
drawUI();
ciphertext = Array.from(buttons.input.value().toUpperCase());
ciphertext = ciphertext.filter(v => alpha.includes(v)).join("");
keyLen = isNaN(parseInt(buttons.len.value())) && 1 || parseInt(buttons.len.value());
block = Math.floor(ciphertext.length/keyLen);
columns = [];
for (let i = 0; i < keyLen; i++) {
columns.push(ciphertext.slice(block * i, block * (i + 1)));
}
}