-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
TrifidCipher.js
198 lines (171 loc) · 5.68 KB
/
TrifidCipher.js
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
import Chain from '../Chain'
import Encoder from '../Encoder'
const meta = {
name: 'trifid-cipher',
title: 'Trifid cipher',
category: 'Polybius square ciphers',
type: 'encoder'
}
/**
* The Trifid alphabet
* @type {string}
*/
const baseAlphabet = 'abcdefghijklmnopqrstuvwxyz+'
/**
* Encoder brick for Trifid cipher encryption and decryption
*/
export default class TrifidCipherEncoder extends Encoder {
/**
* Returns brick meta.
* @return {object}
*/
static getMeta () {
return meta
}
/**
* Constructor
*/
constructor () {
super()
this.addSettings([
{
name: 'key',
type: 'text',
value: '',
uniqueChars: true,
minLength: 0,
maxLength: 27,
caseSensitivity: false
},
{
name: 'groupSize',
type: 'number',
value: 5,
min: 2
}
])
}
/**
* Performs encode on given content.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain} Encoded content
*/
async performEncode (content) {
const { key, groupSize } = this.getSettingValues()
// Derive mixed-alphabet from key
const alphabet = Chain.wrap(key).extend(baseAlphabet).getCodePoints()
// Map Unicode code points to their respective alphabet positions
const positions =
content.toLowerCase().getCodePoints()
.map(codePoint => alphabet.indexOf(codePoint))
.filter(codePoint => codePoint !== -1)
// Delastelle says: 'We start by writing vertically under each letter, the
// numerical trigram that corresponds to it in the enciphering alphabet'
// a i d e t o i l e c
// 1 1 1 1 2 3 1 1 1 2
// 3 2 3 1 1 1 2 1 1 2
// 1 1 3 2 2 1 1 3 2 1
// Thus working with a one-dimensional array to represent the above table
// we enumerate coordinates like this:
// a i d e t o i l e c
// 01 02 03 04 05 21 22 23 24 25
// 06 07 08 09 10 26 27 28 29 30
// 11 12 13 14 15 31 32 33 34 35
// 16 17 18 19 20 36 37 38 39 40
const length = positions.length
const table = new Array(length * 3)
let i, j, coordinates, group, index, size
for (i = 0; i < length; i++) {
// Gather group number, character index inside group and group size, which
// may be shorter for the last group
group = Math.floor(i / groupSize)
index = i - group * groupSize
size = Math.min(groupSize, length - group * groupSize)
// Position group coordinates vertically inside the one-dimensional table
coordinates = this.cartesianCoordinatesFromIndex(positions[i])
for (j = 0; j < 3; j++) {
table[index + group * groupSize * 3 + size * j] = coordinates[j]
}
}
// Following Delastelle: 'Proceeding horizontally as if the numbers were
// written on a single line, we take groups of three numbers, look them up
// in the deciphering alphabet'
// Having built a one-dimensional array, we read 3-element cartesian
// coordinates successively, translate them to one-dimensional indexes and
// map each of them to the corresponding alphabet character
const result = new Array(length)
for (i = 0; i < length; i++) {
coordinates = table.slice(i * 3, (i + 1) * 3)
index = this.indexFromCartesianCoordinates(coordinates)
result[i] = alphabet[index]
}
return result
}
/**
* Performs decode on given content.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain} Decoded content
*/
async performDecode (content) {
const { key, groupSize } = this.getSettingValues()
// Derive mixed-alphabet from key
const alphabet = Chain.wrap(key).extend(baseAlphabet).getCodePoints()
// Map Unicode code points to their respective alphabet positions
const positions =
content.toLowerCase().getCodePoints()
.map(codePoint => alphabet.indexOf(codePoint))
.filter(codePoint => codePoint !== -1)
// Fill in table horizontally
// Refer to the `performEncode` method for more detailed explanations
const length = positions.length
const table = new Array(length * 3)
let i, j, coordinates
for (i = 0; i < length; i++) {
coordinates = this.cartesianCoordinatesFromIndex(positions[i])
for (j = 0; j < 3; j++) {
table[i * 3 + j] = coordinates[j]
}
}
// Read trigrams vertically for each character
const result = new Array(length)
let group, index, size
for (i = 0; i < length; i++) {
// Gather group facts
group = Math.floor(i / groupSize)
index = i - group * groupSize
size = Math.min(groupSize, length - group * groupSize)
// Read one character from vertical trigram
for (j = 0; j < 3; j++) {
coordinates[j] = table[index + group * groupSize * 3 + size * j]
}
index = this.indexFromCartesianCoordinates(coordinates)
result[i] = alphabet[index]
}
return result
}
/**
* Translates a one-dimensional index to three-dimensional cartesian
* coordinates for a 3×3×3 cube.
* @protected
* @param {number} index Index
* @return {number[]} Cartesian coordinate positions (x, y, z)
*/
cartesianCoordinatesFromIndex (index) {
let x = Math.floor(index / 9)
let y = Math.floor((index - x * 9) / 3)
let z = index - x * 9 - y * 3
return [x + 1, y + 1, z + 1]
}
/**
* Translates three-dimensional cartesian coordinates to a one-dimensional
* index for a 3×3×3 cube.
* @protected
* @param {number[]} coordinates Cartesian coordinate positions (x, y, z)
* @return {number} Index
*/
indexFromCartesianCoordinates ([x, y, z]) {
return (x - 1) * 9 + (y - 1) * 3 + (z - 1)
}
}