diff --git a/.mocharc.yml b/.mocharc.yml index 2209c5d..5d24e44 100644 --- a/.mocharc.yml +++ b/.mocharc.yml @@ -1,4 +1,4 @@ full-trace: true spec: './lib/**/*.spec.js' bail: true -slow: 12000 \ No newline at end of file +slow: 6000 \ No newline at end of file diff --git a/lib/enigma-cracker.js b/lib/enigma-cracker.js index 2674359..2ace93e 100644 --- a/lib/enigma-cracker.js +++ b/lib/enigma-cracker.js @@ -6,7 +6,67 @@ const { Enigma } = require('./enigma'); class EnigmaCracker { constructor() { + this.standardAlphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; this.rotorOptions = this.getAllRotorSettings(); + this.plugboardPairs = this.getAllPlugboardPairs(); + } + + decrypt(text) { + const bestRotorSettings = this.getBestRotorSettings(text); + const bestPlugboardSettings = this.getBestPlugboardSettings(bestRotorSettings[0].rotorSetting, text); + + return new Enigma(bestRotorSettings[0].rotorSetting, bestPlugboardSettings).encrypt(text); + } + + getBestPlugboardSettings(rotorSetting, text) { + + let charsStr = ''; + let blacklistedCharacters = []; + const pairs = Object.assign([], this.plugboardPairs); + const candidates = pairs.map( + (pair) => { + const anEnigma = new Enigma(rotorSetting, pair); + + return { + pair, + ioc: this.calculateIoc( anEnigma.encrypt(text) ) + }; + } + ).sort((a, b) => b.ioc - a.ioc) + .filter( + (candidate) => { + + // if not already used, then add to list of likely character swaps + let parts = candidate.pair.split(''); + parts = parts.filter( + part => { + return blacklistedCharacters.indexOf(part) != -1 + } + ); + + if(parts.length < 1) { + + const newCharsStr = charsStr + candidate.pair + ' '; + // check if it improves the baseline + const baselineIoc = this.calculateIoc( new Enigma(rotorSetting, charsStr).encrypt(text) ); + const candidateIoc = this.calculateIoc( new Enigma(rotorSetting, newCharsStr).encrypt(text) ); + + if(baselineIoc < candidateIoc) { + + charsStr = newCharsStr + ''; + parts = candidate.pair.split(''); + + blacklistedCharacters = blacklistedCharacters.concat( parts ); + + return true; + } + } + + return false; + } + ); + + return charsStr; } getBestRotorSettings(text) { @@ -21,7 +81,30 @@ class EnigmaCracker { } ) .sort((a, b) => { return b.iocRating - a.iocRating } ) - .slice(0, 3); + .slice(0, 10); + } + + getAllPlugboardPairs() { + + let initialIndex = 0; + let supplementalIndex = 1; + const pairs = []; + + while(initialIndex != this.standardAlphabet.length - 1) { + pairs.push( + this.standardAlphabet[initialIndex] + + this.standardAlphabet[supplementalIndex] + ); + + supplementalIndex++; + + if(supplementalIndex === this.standardAlphabet.length) { + initialIndex++; + supplementalIndex = (initialIndex + 1); + } + } + + return pairs; } /** diff --git a/lib/enigma-cracker.spec.js b/lib/enigma-cracker.spec.js index cfa3a46..b144c1a 100644 --- a/lib/enigma-cracker.spec.js +++ b/lib/enigma-cracker.spec.js @@ -7,20 +7,21 @@ const { EnigmaCracker } = require('./enigma-cracker'); describe('Class: EnigmaCracker', () => { - it('should identify the correct rotor settings', (done) => { - - const aCracker = new EnigmaCracker(); - const testRotorSetting = [ 19, 12, 25 ]; + it('should correctly decrypt enigma text', (done) => { fs.readFile(path.resolve(__dirname, 'text/secret.txt'), 'utf8', (err, txt) => { - const anEnigma = new Enigma(testRotorSetting); + // encrypt it + const anEnigma = new Enigma([ 19, 12, 25 ], 'qw er ty ui op as df gh jk zx'); const encryptedText = anEnigma.encrypt(txt); - const results = aCracker.getBestRotorSettings(encryptedText); + // crack it... + const aCracker = new EnigmaCracker(); + const result = aCracker.decrypt(encryptedText); + const sanitizedTxt = aCracker.sanitizeText(txt); - expect(results[0].rotorSetting).deep.equals(testRotorSetting); + expect(result).equals(sanitizedTxt); done(); }); - }).timeout(60000); + }).timeout(120000); }); \ No newline at end of file diff --git a/lib/text/secret.txt b/lib/text/secret.txt index 46c8e1b..9d117d0 100644 --- a/lib/text/secret.txt +++ b/lib/text/secret.txt @@ -1,3 +1,5 @@ Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation, or any nation so conceived and so dedicated, can long endure. We are met on a great battle-field of that war. We have come to dedicate a portion of that field, as a final resting place for those who here gave their lives that that nation might live. It is altogether fitting and proper that we should do this. -But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth. \ No newline at end of file +But, in a larger sense, we can not dedicate -- we can not consecrate -- we can not hallow -- this ground. The brave men, living and dead, who struggled here, have consecrated it, far above our poor power to add or detract. The world will little note, nor long remember what we say here, but it can never forget what they did here. It is for us the living, rather, to be dedicated here +to the unfinished work which they who fought here have thus far so nobly advanced. It is rather for us to be here dedicated to the great task remaining before us -- that from these honored dead we take increased devotion to that cause for which they gave the last full measure of devotion -- that we here highly resolve that these dead shall not have died in vain -- that this nation, +under God, shall have a new birth of freedom -- and that government of the people, by the people, for the people, shall not perish from the earth. diff --git a/package.json b/package.json index 91250f6..7d117d2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "enigma", - "version": "1.0.0", + "version": "1.1.0", "description": "", "main": "enigma.js", "scripts": {