-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhochzeit.js
75 lines (60 loc) · 2.12 KB
/
hochzeit.js
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
const fs = require('fs');
const { exit } = require('process');
var seedrandom = require('seedrandom');
var rng = seedrandom('20201009');
const hintText = fs.readFileSync('hinttext.txt').toString();
const solutionText = fs.readFileSync('solutiontext.txt').toString();
const plainAlphabet = []
// read all characters from hintText into the plainAlphabet
for (let i=0;i<hintText.length;i++)
{
const c = hintText.charAt(i);
if (!plainAlphabet.includes(c) && c != '\n')
plainAlphabet.push(c)
}
// Shuffle plain alphabet to lose context
plainAlphabet.sort(() => rng() - 0.5)
//generate a random cipher alphabet by copying and shuffling the plain alphabet again
const cipherAlphabet = Array.from(plainAlphabet).sort(() => rng() - 0.5)
// Assemble the Cipher -> Plaintext mapping
let solutionMapping = 'Ciphertext -> Klartext\n'
for (let i = 0; i < cipherAlphabet.length; i++) {
const line = cipherAlphabet[i] + ' -> ' + plainAlphabet[i];
solutionMapping += line + '\n';
}
fs.writeFileSync('mapping.txt', solutionMapping);
// Assemble the empty mapping, so it can be filled out easily on a piece of paper
let solutionMappingEmpty = 'Ciphertext -> Klartext\n'
for (let i = 0; i < cipherAlphabet.length; i++) {
const line = cipherAlphabet[i] + ' -> __';
solutionMappingEmpty += line + '\n';
}
fs.writeFileSync('mappingEmpty.txt', solutionMappingEmpty);
// now, encrypt both texts with the mapping
let ciphersolutionText = encrypt(solutionText);
let cipherhintText = encrypt(hintText);
fs.writeFileSync('ciphersolutiontext.txt', ciphersolutionText);
fs.writeFileSync('cipherhinttext.txt', cipherhintText);
function encrypt(plaintext)
{
let cipherText = '';
for (let i = 0; i < plaintext.length; i++) {
const c = plaintext[i];
if (c=='\n')
{
cipherText += c;
continue;
}
const index = plainAlphabet.indexOf(c);
if (index != -1)
{
cipherText += cipherAlphabet[index];
}
else
{
console.error('Error: no mapping for character \'' + c + '\'');
exit(-1);
}
}
return cipherText;
}