-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnigma-Cipher.js
More file actions
474 lines (462 loc) · 16.6 KB
/
Copy pathEnigma-Cipher.js
File metadata and controls
474 lines (462 loc) · 16.6 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
const crypto = require('crypto')
const base62chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
/**
* Encode Buffer To Base62
* Params: InputBuffer
*/
function base62EncodeBuffer(inputBuffer) {
let numericValue = 0n
for (const eachByte of inputBuffer) {
numericValue = (numericValue << 8n) | BigInt(eachByte)
}
let outputString = ''
if (numericValue === 0n) {
return '0'
} else {
while (numericValue > 0n) {
outputString = base62chars[numericValue % 62n] + outputString
numericValue = numericValue / 62n
}
}
return outputString.padStart(Math.ceil(inputBuffer.length * 1.37), '0')
}
/**
* Decode Base62 To Buffer
* Params: InputString
*/
function base62DecodeString(inputString) {
let numericValue = 0n
for (const eachChar of inputString) {
numericValue = numericValue * 62n + BigInt(base62chars.indexOf(eachChar))
}
let outputBuffer = []
let bufferLength = Math.floor(inputString.length / 1.37)
while (numericValue > 0n && outputBuffer.length < 64) {
outputBuffer.unshift(Number(numericValue & 0xFFn))
numericValue >>= 8n
}
while (outputBuffer.length < bufferLength) {
outputBuffer.unshift(0)
}
return Buffer.from(outputBuffer)
}
/**
* Convert Number To Base62 String
* Params: InputNumber
*/
function toBase62String(inputNumber) {
let outputString = ''
do {
outputString = base62chars[inputNumber % 62] + outputString
inputNumber = Math.floor(inputNumber / 62)
} while (inputNumber > 0)
return outputString.padStart(2, '0')
}
/**
* Convert Base62 String To Number
* Params: InputString
*/
function fromBase62String(inputString) {
let resultNumber = 0
for (const eachChar of inputString) {
resultNumber = resultNumber * 62 + base62chars.indexOf(eachChar)
}
return resultNumber
}
/**
* Shuffle Array By Seed
* Params: InputArray, SeedArray
*/
function shuffleArrayBySeed(inputArray, seedArray) {
let resultArray = inputArray.slice()
let numericArray
if (Buffer.isBuffer(seedArray)) {
numericArray = Array.from(seedArray)
} else if (typeof seedArray === 'bigint') {
let tempSeed = seedArray
numericArray = []
while (tempSeed > 0n) {
numericArray.unshift(Number(tempSeed & 0xFFn))
tempSeed >>= 8n
}
} else {
numericArray = Array.from(seedArray)
}
for (let index = resultArray.length - 1; index > 0; index--) {
let swapIndex = numericArray[index % numericArray.length] % (index + 1)
let tempValue = resultArray[index]
resultArray[index] = resultArray[swapIndex]
resultArray[swapIndex] = tempValue
}
return resultArray
}
/**
* Generate Random Buffer
* Params: Length
*/
function generateRandomBuffer(length) {
return crypto.randomBytes(length)
}
/**
* Generate Random Integer
* Params: MaxNumber
*/
function generateRandomInteger(maxNumber) {
return generateRandomBuffer(2).readUInt16BE(0) % maxNumber
}
/**
* Derive Key By Passphrase And Purpose
* Params: Passphrase, Purpose, Length
*/
function deriveKeyFromPassphrase(passedPhrase, passedPurpose, passedLength = 32) {
return crypto.createHash('sha256').update(passedPhrase + ':' + passedPurpose).digest().slice(0, passedLength)
}
/**
* Plugboard Class
* Params: MappingPairs
*/
class Plugboard {
constructor(mappingPairs) {
this.mappingObject = {}
for (const eachPair of mappingPairs) {
if (eachPair.length === 2) {
this.mappingObject[eachPair[0]] = eachPair[1]
this.mappingObject[eachPair[1]] = eachPair[0]
}
}
}
swap(character) {
return this.mappingObject[character] || character
}
static randomPairs(pairCount = 6) {
const alphabetArray = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
const shuffledArray = shuffleArrayBySeed(alphabetArray, generateRandomBuffer(alphabetArray.length))
let pairsArray = []
for (let index = 0; index < pairCount * 2; index += 2) {
if (shuffledArray[index + 1]) {
pairsArray.push(shuffledArray[index] + shuffledArray[index + 1])
}
}
return pairsArray
}
static serialize(pairsArray) {
return pairsArray.join('')
}
static deserialize(pairString) {
let resultPairs = []
for (let index = 0; index < pairString.length - 1; index += 2) {
resultPairs.push(pairString[index] + pairString[index + 1])
}
return resultPairs
}
}
/**
* Rotor Class
* Params: WiringString, NotchIndex, RotorPosition
*/
class Rotor {
constructor(wiringString, notchIndex, rotorPosition) {
this.forward = wiringString
this.backward = Array(26)
for (let index = 0; index < 26; index++) {
this.backward[this.forward.charCodeAt(index) - 65] = String.fromCharCode(65 + index)
}
this.notch = notchIndex
this.position = rotorPosition
}
step() {
this.position = (this.position + 1) % 26
}
encodeForward(character) {
let characterIndex = (character.charCodeAt(0) - 65 + this.position) % 26
return this.forward[characterIndex]
}
encodeBackward(character) {
let characterIndex = (this.forward.indexOf(character) - this.position + 26) % 26
return String.fromCharCode(65 + characterIndex)
}
}
/**
* Enigma Cipher Super Class
* Params: OptionsObject
*/
class EnigmaCipherSuper {
constructor(optionsObject = {}) {
this.rotorWiringArray = [
'EKMFLGDQVZNTOWYHXUSPAIBRCJ',
'AJDKSIRUXBLHWTMCQGZNPYFVOE',
'BDFHJLCPRTXVZNYEIWGAKMUSQO'
]
this.rotorNotchArray = [16, 4, 21]
this.reflectorString = 'YRUHQSLDPXNGOKMIEBFZCWVJAT'
this.hashAlgorithm = 'sha256'
this.macLength = 8
this.defaultSaltLength = 3
this.plugboardPairCount = 6
this.passphrase = optionsObject.passphrase || optionsObject.pass || null
this.saltLengthChar = 3
this.plugboardLengthChar = this.plugboardPairCount * 2
this.positionLengthChar = 6
this.orderLengthChar = 3
this.parameterLength = this.saltLengthChar + this.plugboardLengthChar + this.positionLengthChar + this.orderLengthChar
this.shuffleKeyLength = 11
this.stealthMode = optionsObject.stealth || false
this.pass = optionsObject.pass || null
}
/**
* Get Rotors
* Params: RotorPositions, WiringOrderArray
*/
getRotors(rotorPositions, wiringOrderArray) {
return [
new Rotor(this.rotorWiringArray[wiringOrderArray[0]], this.rotorNotchArray[wiringOrderArray[0]], rotorPositions[0]),
new Rotor(this.rotorWiringArray[wiringOrderArray[1]], this.rotorNotchArray[wiringOrderArray[1]], rotorPositions[1]),
new Rotor(this.rotorWiringArray[wiringOrderArray[2]], this.rotorNotchArray[wiringOrderArray[2]], rotorPositions[2])
]
}
/**
* Get Salt Buffer
* Params: None
*/
getSaltBuffer() {
if (this.passphrase || this.pass) {
const derivedHash = deriveKeyFromPassphrase(this.passphrase || this.pass, 'salt', 32)
return derivedHash.slice(0, this.defaultSaltLength)
} else {
return generateRandomBuffer(this.defaultSaltLength)
}
}
/**
* Get Plugboard Pairs
* Params: None
*/
getPlugboardPairs() {
if (this.passphrase || this.pass) {
const derivedHash = deriveKeyFromPassphrase(this.passphrase || this.pass, 'plugboard', 32)
const alphabetArray = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('')
const shuffledArray = shuffleArrayBySeed(alphabetArray, derivedHash)
let pairsArray = []
for (let index = 0; index < this.plugboardPairCount * 2; index += 2) {
if (shuffledArray[index + 1]) {
pairsArray.push(shuffledArray[index] + shuffledArray[index + 1])
}
}
return pairsArray
}
return Plugboard.randomPairs(this.plugboardPairCount)
}
/**
* Get Random Rotor Positions
* Params: None
*/
getRandomRotorPositions() {
if (this.passphrase || this.pass) {
const derivedHash = deriveKeyFromPassphrase(this.passphrase || this.pass, 'rotor', 32)
return [derivedHash[0] % 26, derivedHash[1] % 26, derivedHash[2] % 26]
}
return [generateRandomInteger(26), generateRandomInteger(26), generateRandomInteger(26)]
}
/**
* Get Random Wiring Order
* Params: None
*/
getRandomWiringOrder() {
if (this.passphrase || this.pass) {
const derivedHash = deriveKeyFromPassphrase(this.passphrase || this.pass, 'order', 32)
return shuffleArrayBySeed([0, 1, 2], derivedHash)
}
return shuffleArrayBySeed([0, 1, 2], generateRandomBuffer(8))
}
/**
* Clean Output String
* Params: InputString
*/
cleanOutputString(inputString) {
return (inputString.match(/[0-9A-Za-z]+/g) || []).join('')
}
/**
* Encrypt Plain Text
* Params: PlainText
*/
encrypt(plainText) {
const saltBuffer = this.getSaltBuffer()
const plugboardPairs = this.getPlugboardPairs()
const rotorPositions = this.getRandomRotorPositions()
const wiringOrder = this.getRandomWiringOrder()
const plugboard = new Plugboard(plugboardPairs)
const saltString = base62EncodeBuffer(saltBuffer).padEnd(this.saltLengthChar, '0').slice(0, this.saltLengthChar)
const plugboardString = Plugboard.serialize(plugboardPairs).padEnd(this.plugboardLengthChar, 'A').slice(0, this.plugboardLengthChar)
const positionString = rotorPositions.map(toBase62String).join('').padEnd(this.positionLengthChar, '0').slice(0, this.positionLengthChar)
const orderString = wiringOrder.map(String).join('').padEnd(this.orderLengthChar, '0').slice(0, this.orderLengthChar)
const parameterString = saltString + plugboardString + positionString + orderString
const shuffleSeed = generateRandomBuffer(8)
const shuffleKey = base62EncodeBuffer(shuffleSeed).slice(0, this.shuffleKeyLength)
const parameterArray = parameterString.split('')
const obfuscatedParameterArray = shuffleArrayBySeed(parameterArray, shuffleSeed)
const obfuscatedParameterString = obfuscatedParameterArray.join('')
let swappedPlainText = ''
for (const character of plainText.toUpperCase()) {
swappedPlainText += plugboard.swap(character)
}
const rotors = this.getRotors(rotorPositions, wiringOrder)
let cipherText = ''
for (const character of swappedPlainText) {
if (!( /[A-Z]/.test(character))) {
cipherText += character
} else {
rotors[2].step()
if (rotors[2].position === rotors[2].notch) {
rotors[1].step()
if (rotors[1].position === rotors[1].notch) {
rotors[0].step()
}
}
let codeChar = character
for (let index = 2; index >= 0; index--) {
codeChar = rotors[index].encodeForward(codeChar)
}
codeChar = this.reflectorString[codeChar.charCodeAt(0) - 65]
for (let index = 0; index < 3; index++) {
codeChar = rotors[index].encodeBackward(codeChar)
}
cipherText += codeChar
}
}
let swappedCipherText = ''
for (const character of cipherText) {
swappedCipherText += plugboard.swap(character)
}
const cipherLength = swappedCipherText.length
const cipherEncoded = base62EncodeBuffer(Buffer.from(swappedCipherText, 'ascii'))
const cipherLengthString = toBase62String(cipherLength).padStart(2, '0')
let hmacKey
if (this.passphrase) {
hmacKey = deriveKeyFromPassphrase(this.passphrase, 'mac', 32)
} else if (this.pass) {
hmacKey = deriveKeyFromPassphrase(this.pass, 'mac', 32)
} else {
hmacKey = saltBuffer
}
let macString = crypto.createHmac('sha256', hmacKey)
.update(cipherEncoded + cipherLengthString + obfuscatedParameterString + shuffleKey)
.digest('base64').slice(0, this.macLength)
let mainBodyString = shuffleKey + obfuscatedParameterString + cipherEncoded + cipherLengthString + macString
if (this.stealthMode) {
let padBuffer = generateRandomBuffer(mainBodyString.length)
let padString = base62EncodeBuffer(padBuffer).slice(0, mainBodyString.length)
let stealthBodyString = ''
for (let index = 0; index < mainBodyString.length; index++) {
stealthBodyString += base62chars[(base62chars.indexOf(mainBodyString[index]) + base62chars.indexOf(padString[index])) % 62]
}
mainBodyString = stealthBodyString + padString
}
return this.cleanOutputString(mainBodyString)
}
/**
* Decrypt Cipher Text
* Params: CipherText
*/
decrypt(cipherText) {
cipherText = this.cleanOutputString(cipherText)
let mainBodyString = cipherText
if (this.stealthMode) {
let n = mainBodyString.length / 2
let stealthBodyString = mainBodyString.slice(0, n)
let padString = mainBodyString.slice(n)
let originalString = ''
for (let index = 0; index < n; index++) {
let resultIndex = (base62chars.indexOf(stealthBodyString[index]) - base62chars.indexOf(padString[index]) + 62) % 62
originalString += base62chars[resultIndex]
}
mainBodyString = originalString
}
const shuffleKey = mainBodyString.slice(0, 11)
const shuffleSeed = base62DecodeString(shuffleKey)
const obfuscatedParameterString = mainBodyString.slice(11, 35)
const cipherLengthString = mainBodyString.slice(-10, -8)
const cipherLength = fromBase62String(cipherLengthString)
const cipherEncoded = mainBodyString.slice(35, -10)
const macString = mainBodyString.slice(-8)
let parameterArray = obfuscatedParameterString.split('')
let sortedIndexArray = Array(parameterArray.length).fill(0).map((_, index) => index)
let indexOrderArray = shuffleArrayBySeed(sortedIndexArray, shuffleSeed)
let parameterResultArray = Array(obfuscatedParameterString.length)
for (let index = 0; index < indexOrderArray.length; index++) {
parameterResultArray[indexOrderArray[index]] = obfuscatedParameterString[index]
}
const parameterString = parameterResultArray.join('')
let plugboardString = parameterString.slice(3, 15)
let positionString = parameterString.slice(15, 21)
let orderString = parameterString.slice(21, 24)
const plugboardPairs = Plugboard.deserialize(plugboardString)
const rotorPositions = [fromBase62String(positionString.slice(0, 2)), fromBase62String(positionString.slice(2, 4)), fromBase62String(positionString.slice(4, 6))]
const wiringOrder = orderString.split('').map(Number)
const plugboard = new Plugboard(plugboardPairs)
let hmacKey
if (this.passphrase) {
hmacKey = deriveKeyFromPassphrase(this.passphrase, 'mac', 32)
} else if (this.pass) {
hmacKey = deriveKeyFromPassphrase(this.pass, 'mac', 32)
} else {
hmacKey = base62DecodeString(parameterString.slice(0, 3))
}
let macCheckString = crypto.createHmac('sha256', hmacKey)
.update(cipherEncoded + cipherLengthString + obfuscatedParameterString + shuffleKey)
.digest('base64').slice(0, this.macLength)
if (macString !== macCheckString) {
throw new Error('MAC integrity check failed!')
}
const decodedString = base62DecodeString(cipherEncoded).slice(0, cipherLength).toString('ascii').replace(/\0+$/, '')
let afterPlugString = ''
for (const character of decodedString) {
afterPlugString += plugboard.swap(character)
}
const rotors = this.getRotors(rotorPositions, wiringOrder)
let outputString = ''
for (const character of afterPlugString) {
if (!( /[A-Z]/.test(character))) {
outputString += character
} else {
rotors[2].step()
if (rotors[2].position === rotors[2].notch) {
rotors[1].step()
if (rotors[1].position === rotors[1].notch) {
rotors[0].step()
}
}
let codeChar = character
for (let index = 2; index >= 0; index--) {
codeChar = rotors[index].encodeForward(codeChar)
}
codeChar = this.reflectorString[codeChar.charCodeAt(0) - 65]
for (let index = 0; index < 3; index++) {
codeChar = rotors[index].encodeBackward(codeChar)
}
outputString += codeChar
}
}
let plainString = ''
for (const character of outputString) {
plainString += plugboard.swap(character)
}
return plainString
}
}
if (require.main === module) {
const argumentList = process.argv.slice(2)
let commandMode = argumentList[0]
let stealthOption = argumentList.includes('--stealth')
let passOptionIndex = argumentList.findIndex(eachArgument => eachArgument === '--pass')
let passOption = passOptionIndex !== -1 ? argumentList[passOptionIndex + 1] : null
let inputText = argumentList.slice(1).filter(argumentValue => !(argumentValue.startsWith('--')) && argumentValue !== passOption).join(' ')
const enigmaCipher = new EnigmaCipherSuper({ pass: passOption, stealth: stealthOption })
if (commandMode === 'encrypt') {
console.log(enigmaCipher.encrypt(inputText))
} else if (commandMode === 'decrypt') {
try {
console.log(enigmaCipher.decrypt(inputText))
} catch (errorObject) {
console.log('FAILED: MAC integrity check failed!')
}
}
}