-
Notifications
You must be signed in to change notification settings - Fork 0
/
decryption.js
88 lines (70 loc) · 1.91 KB
/
decryption.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function addListeners(){
var button_count = document.getElementById('button_count');
if(window.addEventListener) {
button_count.addEventListener("click", callback("count"), false);
}
}
String.prototype.cleanup = function() {
return this.toUpperCase().replace(/[^a-zA-Z]+/g, "");
}
function callback(action){
// var cipherText = "JBDYZCLNHJBDOR";
var cipherText = document.getElementById("message").value.cleanup();
return function(){
var sequenceArray = [];
var k = 3; //keylength
var count = 0;
for(var pos=0; pos<cipherText.length-k; pos++){
var sequence = cipherText.substring(pos, pos+k);
let obj = sequenceArray.find(obj => obj.sequence == sequence);
if(obj){
obj.positions.push(pos);
}else{
sequenceArray.push({sequence:sequence, positions:[pos]});
}
}
//.filter(obj => obj.positions > 3)
var gt4coinc = sequenceArray.filter(obj => obj.positions.length > 3);
gt4coinc.forEach(printObj);
//get all numbers
var num = [];
gt4coinc.forEach(obj => num.push(obj.positions));
//console.log("num : " + num);
num = num.flat();
var gcds = [];
for(i=0; i<num.length-1; i++){
gcds.push(gcd(num[i],num[i+1]));
}
console.log("gcds: " + gcds);
}
}
function printObj(item, index){
console.log(JSON.stringify(item));
}
//greatest common divisor
function gcd(a,b){
//console.log("ab:" + a + "," + b);
if ( ! b) {
return a;
}
return gcd(b, a % b);
}
function old_callback(action){
// var cipherText = "JBDYZCLNHJBDOR";
var cipherText = document.getElementById("message").value.cleanup();
return function(){
var ary = {};
var k = 3; //keylength
var count = 0;
for(var pos=0; pos<cipherText.length-k; pos++){
var ss = cipherText.substring(pos, pos+k);
if(ary[ss] == undefined){
ary[ss] = pos;
}else{
ary[ss] += "," + pos;
}
}
console.log(ary.filter((s,p) => p.length > 12));
}
}
window.onload = addListeners;