-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_script.js
More file actions
74 lines (65 loc) · 2.1 KB
/
Copy pathmain_script.js
File metadata and controls
74 lines (65 loc) · 2.1 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
72
73
74
/*
* Encoding:
* Calculate how many times the passphrase will be stirred and XOR'ed against the text as the fourth root of the length of the message +1
* Stir the passphrase - cut the first char and join it to the end
* Build the key - repeat the edited passphrase to match the length of the text
* XOR the key against the text; return the result
* Grab the passphrase edited in step 2 and the result from step 4; repeat steps 2-4 as many times as set in step 1
*
* Decoding:
* Magic.
*/
'use strict';
var encoding_rounds = 4;
/*
** Build a key with a length equal to the initial text
*/
var build_cyph = function(pass, length) {
var cyph = '';
for(var i=0; i<length; i++) {
var len = pass.length;
cyph += pass.charAt(i % len);
}
return cyph;
}
/*
** Encode
*/
var cypher = function(message, cyph) {
//Calculate how many times the passphrase will be stirred
encoding_rounds = Math.floor(Math.sqrt(Math.sqrt(message.length)))+1;
for( var j=0; j < encoding_rounds; j++ ) {
//Build a key by sending the first char to the
cyph = cyph.slice(1) + cyph.slice(0,1);
var key = build_cyph(cyph, message.length);
//XOR the chars one-by-one
for( var i=0, xor = ''; i<message.length; i++ ) {
var c = message.charCodeAt(i);
var d = key.charCodeAt(i);
var next_char_xor = String((c ^ d)+33);
next_char_xor = String.fromCharCode(next_char_xor);
xor += next_char_xor;
}
}
//Voila!
return xor;
}
/*
** Decode - reversing the above
*/
var reverse_xor = function(gibberish,pass) {
encoding_rounds = Math.floor(Math.sqrt(Math.sqrt(gibberish.length)))+1;
for( var j=0; j<encoding_rounds; j++ ) {
var decyphered = '';
//Redundant piece below, to be fixed
pass = pass.slice(1) + pass.slice(0,1);
console.log('pass '+j+'. '+pass);
var key = build_cyph(pass, gibberish.length);
for(var i=0; i<gibberish.length; i++) {
var gibb_current_char = gibberish.charCodeAt(i)-33;
var key_current_char = key.charCodeAt(i);
decyphered += String.fromCharCode(gibb_current_char ^ key_current_char);
}
}
return decyphered;
}