-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcipher.js
More file actions
71 lines (60 loc) · 1.91 KB
/
cipher.js
File metadata and controls
71 lines (60 loc) · 1.91 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
'use strict'
const analyze = require('./modules/analyze');
const decode = require('./modules/decode');
const initialize = require('./modules/initialize');
const terminate = require('./modules/terminate');
/*
* There are two phases to figuring this out:
*
* 1. Analyze some English text to for patterns.
* It counts all the letters and all the pairs of letters
* and then calculates the frequency at which they appear.
* So, the pattern is frequency of occurence.
*
* 2. Decode the encrypted text. There are three parts:
*
* a.) Analyze the encrypted text. This is the same as
* analyzing the English text. Just count the letters
* and pairs, and then calculate the frequency of
* occurence.
*
* b.) Figure out the cipher. By putting the frequencies
* in order, you can figure out a one-to-one corresondence.
* If just doing the letter frequencies doesn't make it
* clear, you can do the pairs.
*
* c.) Use the cipher to decrypt the encrypted text.
*
* This code has a big JSON object, oConfig, it carries around
* with it. At each stage more information is put in the oConfig object
* until it has everything in it.
*
* This is designed so that by default it will analyze a file called
* 'plain.txt', and decrypt a file called 'encrypted.txt'. If those
* files are in place, you can type:
*
* node cipher.js
*
* from the command line, and it should work ... provided you have
* node installed and have done npm install.
*/
// TO DO - Grow up! Use promises.:w
function step4(err, oConfig) { // JavaScript is upside down
terminate(err, oConfig);
}
function step3(err, oConfig) {
if (err) {
terminate(err, oConfig);
}
decode(oConfig, step4);
}
function step2(err, oConfig) {
if (err) {
terminate(err, oConfig);
}
analyze.plain(oConfig, step3);
}
function step1() {
initialize(step2)
}
step1();