-
-
Notifications
You must be signed in to change notification settings - Fork 241
/
NihilistCipher.js
191 lines (166 loc) · 4.86 KB
/
NihilistCipher.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
import Chain from '../Chain'
import Encoder from '../Encoder'
import InvalidInputError from '../Error/InvalidInput'
import MathUtil from '../MathUtil'
import PolybiusSquareEncoder from './PolybiusSquare'
const meta = {
name: 'nihilist-cipher',
title: 'Nihilist cipher',
category: 'Polybius square ciphers',
type: 'encoder'
}
/**
* Default key
* @type {string}
*/
const defaultKey = 'cryptii'
/**
* Default alphabet (without J)
* @type {string}
*/
const alphabet = 'abcdefghiklmnopqrstuvwxyz'
/**
* Encoder brick for Nihilist cipher encryption and decryption
*/
export default class NihilistCipherEncoder extends Encoder {
/**
* Returns brick meta.
* @return {object}
*/
static getMeta () {
return meta
}
/**
* Constructor
*/
constructor () {
super()
this.addSettings([
{
name: 'alphabet',
type: 'text',
value: alphabet,
uniqueChars: true,
minLength: 0,
maxLength: 25,
caseSensitivity: false
},
{
name: 'key',
type: 'text',
value: defaultKey,
whitelistChars: alphabet,
minLength: 2,
caseSensitivity: false
},
{
name: 'separator',
type: 'text',
value: ' ',
randomizable: false,
blacklistChars: '0123456789',
minLength: 1,
caseSensitivity: false
}
])
// Create internal Polybius square encoder instance
this._polybiusSquare = new PolybiusSquareEncoder()
this._polybiusSquare.setSettingValues({
alphabet: alphabet,
rows: '12345',
columns: '12345',
separator: '',
caseSensitivity: false,
includeForeignChars: false
})
}
/**
* Performs encode on given content.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain} Encoded content
*/
async performEncode (content) {
const key = this.getSettingValue('key')
const separator = this.getSettingValue('separator')
// Translate both plaintext and key using the Polybius square
const plaintextPolybius = await this._polybiusSquare.encode(
content.getString().replace(/j/gi, 'i'))
const keyPolybius = await this._polybiusSquare.encode(key)
const contentLength = plaintextPolybius.getLength() / 2
const keyLength = key.getLength()
const values = new Array(contentLength)
let plaintextValue, keyIndex, keyValue
for (let i = 0; i < contentLength; i++) {
// Compose plaintext number
plaintextValue = parseInt(
plaintextPolybius.getCharAt(i * 2) +
plaintextPolybius.getCharAt(i * 2 + 1)
)
// Compose key number
keyIndex = MathUtil.mod(i, keyLength) * 2
keyValue = parseInt(
keyPolybius.getCharAt(keyIndex) +
keyPolybius.getCharAt(keyIndex + 1)
)
// Add plaintext and key values together
values[i] = plaintextValue + keyValue
}
return Chain.join(values, separator)
}
/**
* Performs decode on given content.
* @protected
* @param {Chain} content
* @return {number[]|string|Uint8Array|Chain} Decoded content
*/
async performDecode (content) {
const key = this.getSettingValue('key')
const separator = this.getSettingValue('separator')
const values = content.getString().split(separator)
const contentLength = values.length
const keyLength = key.getLength()
const keyPolybius = await this._polybiusSquare.encode(key)
let value, plaintextValue, keyIndex, keyValue
let plaintextPolybius = ''
for (let i = 0; i < contentLength; i++) {
// Read next value
value = parseInt(values[i])
if (isNaN(value)) {
throw new InvalidInputError(
`Block at index ${i + 1} is not a number.`)
}
// Compose key number
keyIndex = MathUtil.mod(i, keyLength) * 2
keyValue = parseInt(
keyPolybius.getCharAt(keyIndex) +
keyPolybius.getCharAt(keyIndex + 1)
)
// Compute plaintext number
plaintextValue = value - keyValue
if (!plaintextValue.toString().match(/^[1-5]{2}$/)) {
throw new InvalidInputError(
`Block at index ${i + 1} results in invalid ` +
`Polybius square coordinates '${plaintextValue}'.`)
}
plaintextPolybius += plaintextValue.toString()
}
// Unwrap Polybius square encoded content
return this._polybiusSquare.decode(plaintextPolybius)
}
/**
* Triggered when a setting field has changed.
* @param {Field} setting Sender setting field
* @param {mixed} value New field value
*/
settingValueDidChange (setting, value) {
switch (setting.getName()) {
case 'alphabet':
// Create mixed alphabet
const mixedAlphabet = value.extend(alphabet)
this._polybiusSquare.setSettingValue('alphabet', mixedAlphabet)
this.getSetting('key').setWhitelistChars(mixedAlphabet)
break
}
}
}