Skip to content

Commit

Permalink
Merge pull request #6 from codyjdalton/feature/crack-plugboard
Browse files Browse the repository at this point in the history
Allow cracking plugboard settings as well
  • Loading branch information
codyjdalton authored Apr 28, 2021
2 parents b055c25 + 95717fd commit f365d19
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .mocharc.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
full-trace: true
spec: './lib/**/*.spec.js'
bail: true
slow: 12000
slow: 6000
85 changes: 84 additions & 1 deletion lib/enigma-cracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}

/**
Expand Down
17 changes: 9 additions & 8 deletions lib/enigma-cracker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
4 changes: 3 additions & 1 deletion lib/text/secret.txt
Original file line number Diff line number Diff line change
@@ -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.
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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "enigma",
"version": "1.0.0",
"version": "1.1.0",
"description": "",
"main": "enigma.js",
"scripts": {
Expand Down

0 comments on commit f365d19

Please sign in to comment.