Skip to content

Commit b72a843

Browse files
committed
crack caesar cipher solution
1 parent f2a7eb6 commit b72a843

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
export function encode(word: string, shift: number) {
2+
3+
shift = shift % 26;
4+
5+
return word.split("").map(caesar => {
6+
const ascii: number = caesar.charCodeAt(0);
7+
const new_code: number = ascii + shift;
8+
if (ascii >= 65 && ascii <= 90) {
9+
if (new_code < 65) {return String.fromCharCode(new_code + 26)}
10+
if (new_code > 90) {return String.fromCharCode(new_code - 26)}
11+
else return String.fromCharCode(new_code)
12+
} else if (ascii >= 97 && ascii <= 122) {
13+
if (new_code < 97) {return String.fromCharCode(new_code + 26)}
14+
if (new_code > 122) {return String.fromCharCode(new_code - 26)}
15+
return String.fromCharCode(new_code)
16+
} else return caesar;
17+
}).join('');
18+
}
19+
20+
21+
export function decode(word: string, shift: number) {return encode(word, -shift)}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import {encode} from './caesar-cipher'
2+
import {decode} from './caesar-cipher'
3+
4+
const englishLetterFreq = {'E': 12.70, 'T': 9.06, 'A': 8.17, 'O': 7.51, 'I': 6.97, 'N': 6.75,
5+
'S': 6.33, 'H': 6.09, 'R': 5.99, 'D': 4.25, 'L': 4.03, 'C': 2.78,
6+
'U': 2.76, 'M': 2.41, 'W': 2.36, 'F': 2.23, 'G': 2.02, 'Y': 1.97,
7+
'P': 1.93, 'B': 1.29, 'V': 0.98, 'K': 0.77, 'J': 0.15, 'X': 0.15,
8+
'Q': 0.10, 'Z': 0.07}
9+
10+
const ETAOIN = 'ETAOINSHRDLCUMWFGYPBVKJXQZ'
11+
12+
function getShiftFactor (message: string) {
13+
14+
15+
var letterCount: any = {}
16+
const splitted: any = message.toUpperCase().split("")
17+
18+
for (let letter of splitted) {
19+
if (letter.charCodeAt(0) >= 65 && letter.charCodeAt(0) <= 90) {
20+
if (letter in letterCount) { letterCount[letter] += 1} else {letterCount[letter] = 1}
21+
}
22+
}
23+
24+
const nums = Object.keys(letterCount).map(o => letterCount[o]);
25+
const max = Math.max(...nums);
26+
const char = Object.keys(letterCount).find(letter => letterCount[letter] === max)!;
27+
28+
return (char.charCodeAt(0) - ETAOIN.charCodeAt(0))
29+
30+
}
31+
32+
export function crackCaesar (message: string) {
33+
return decode(message, getShiftFactor(message))
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"use strict";
2+
exports.__esModule = true;
3+
var hack_caesar_1 = require("./hack-caesar");
4+
var fs = require('fs');
5+
var folder = '../../cases/';
6+
var books = ['Shakespeare-Hamlet.txt', 'Shakespeare-Macbeth.txt', 'Shakespeare-Romeo-And-Juliet.txt'];
7+
function decryptFiles(path) {
8+
fs.readFile(path, 'utf8', function (err, data) {
9+
if (err) {
10+
console.error(err);
11+
return;
12+
}
13+
var decrypted_data = (0, hack_caesar_1.crackCaesar)(data);
14+
fs.writeFile(path.slice(0, -4) + '-decrypted.txt', decrypted_data, function (err) {
15+
if (err) {
16+
console.error(err);
17+
return;
18+
}
19+
console.log('file written successfully');
20+
});
21+
});
22+
}
23+
for (var i in books) {
24+
decryptFiles(folder + books[i]);
25+
}

0 commit comments

Comments
 (0)