Skip to content

Commit 2450f24

Browse files
authored
Merge pull request #80 from winsonrich/patch-6
Update keyFinder.js
2 parents 9cabd46 + 2445600 commit 2450f24

File tree

1 file changed

+148
-18
lines changed

1 file changed

+148
-18
lines changed

Ciphers/keyFinder.js

Lines changed: 148 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,155 @@ Find and retrieve the encryption key automatically
33
Note: This is a draft version, please help to modify, Thanks!
44
******************************************************/
55
function keyFinder(str){ // str is used to get the input of encrypted string
6-
var key = 0; // return zero means the key can not be found
7-
var wordbank =["is","Is","am","Am","are","Are","have","Have","has","Has","may","May","be","Be"];
8-
//var shiftNum = 0; //count the number of key shifted
9-
var inStr = str.toString(); //convert the input to String
10-
var outStr = ""; // store the output value
11-
var wordInOutStr = ""; // temporary store the word inside the outStr, it is used for comparison
12-
//document.getElementById("debug").innerHTML = shiftNum; // debug: display the shifted number(s)
13-
for (var i=0; i<(52); i++){ //try the number of key shifted, the sum of character from a-z and A-Z is 26*2=52
14-
outStr = caesarCipherEncodeAndDecodeEngine(inStr,i); // use the encrytpion engine to decrypt the input string, shiftNum=i
15-
for ( var i=0; i<wordbank.length; i++){
16-
// use a loop to find the next digit of wordbank element and compare with outStr's digit
17-
for ( var j=0; j < wordbank[i].length; j++){
18-
wordInOutStr += outStr[i+j];
19-
}
20-
// this part need to be optimize with the calculation of the number of occurance of word's probabilities
21-
if (wordbank[i] == wordInOutStr){
22-
key=i;
6+
const wordbank =["I ","You ","We ","They ","He ","She ","It "," the ","The "," of "," is ","Is "," am ","Am "," are ","Are "," have ","Have "," has ","Has "," may ","May "," be ","Be "];
7+
//let wordbankelementCounter = 0;
8+
//let key = 0; // return zero means the key can not be found
9+
let inStr = str.toString(); //convert the input to String
10+
let outStr = ""; // store the output value
11+
let outStrElement = ""; // temporary store the word inside the outStr, it is used for comparison
12+
for (let k=0; k<26; k++){ //try the number of key shifted, the sum of character from a-z or A-Z is 26
13+
outStr = caesarCipherEncodeAndDecodeEngine(inStr,k); // use the encrytpion engine to decrypt the input string
14+
15+
//loop through the whole input string
16+
for ( let s=0; s < outStr.length; s++){
17+
18+
for ( let i=0; i < wordbank.length; i++){
19+
20+
// initialize the outStrElement which is a temp output string for comparison,
21+
// use a loop to find the next digit of wordbank element and compare with outStr's digit
22+
for ( let w=0; w < wordbank[i].length; w++){
23+
outStrElement += outStr[ s + w ];
24+
}
25+
26+
//console.log( k + outStrElement + wordbank[i] );//debug
27+
28+
// this part need to be optimize with the calculation of the number of occurance of word's probabilities
29+
// linked list will be used in the next stage of development to calculate the number of occurace of the key
30+
if (wordbank[i] == outStrElement){
31+
return k; // return the key number if founded
32+
}
33+
34+
outStrElement = ""; //reset the temp word
35+
36+
} // end for ( let i=0; i < wordbank.length; i++)
37+
38+
}
39+
40+
}
41+
return 0; // return 0 if found nothing
42+
}
43+
44+
/* this sub-function is used to assist the keyfinder to find the key */
45+
function caesarCipherEncodeAndDecodeEngine(inStr, numShifted)
46+
{
47+
let shiftNum = numShifted;
48+
let charCode = 0;
49+
let outStr = "";
50+
let shftedcharCode = 0;
51+
let result = 0;
52+
53+
for (let i=0; i<inStr.length; i++){
54+
55+
charCode = inStr[i].charCodeAt();
56+
shftedcharCode = charCode + shiftNum;
57+
result = charCode;
58+
59+
if ( (charCode>=48 && charCode<=57))
60+
{
61+
if ( shftedcharCode < 48 ){
62+
63+
let diff = Math.abs(48-1-shftedcharCode)%10;
64+
65+
while( diff >= 10){
66+
diff = diff%10;
67+
}
68+
document.getElementById("diffID").innerHTML = diff;
69+
70+
shftedcharCode = 57-diff;
71+
72+
result = shftedcharCode;
73+
}
74+
75+
else if ( shftedcharCode>=48 && shftedcharCode<=57 ){
76+
result = shftedcharCode;
77+
}
78+
79+
else if ( shftedcharCode > 57 ){
80+
81+
let diff = Math.abs(57+1-shftedcharCode)%10;
82+
83+
while( diff >= 10){
84+
diff = diff%10;
85+
}
86+
document.getElementById("diffID").innerHTML = diff;
87+
88+
shftedcharCode = 48+diff;
89+
90+
result = shftedcharCode;
91+
}
92+
93+
94+
}
95+
96+
else if ( (charCode>=65 && charCode<=90) )
97+
{
98+
99+
if (shftedcharCode <=64 ){
100+
101+
let diff = Math.abs(65-1-shftedcharCode)%26;
102+
103+
while( (diff%26) >= 26){
104+
diff = diff%26;
105+
}
106+
shftedcharCode = 90-diff;
107+
result = shftedcharCode;
108+
}
109+
110+
else if ( shftedcharCode>=65 && shftedcharCode<=90 ){
111+
result = shftedcharCode;
112+
}
113+
114+
else if (shftedcharCode>90 ){
115+
let diff = Math.abs(shftedcharCode-1-90)%26;
116+
117+
while( (diff%26) >= 26){
118+
diff = diff%26;
119+
}
120+
shftedcharCode = 65+diff;
121+
result = shftedcharCode;
122+
}
123+
124+
}
125+
126+
else if ( (charCode>=97 && charCode<=122))
127+
{
128+
if ( shftedcharCode<=96 ){
129+
130+
let diff = Math.abs(97-1-shftedcharCode)%26;
131+
132+
while( (diff%26) >= 26){
133+
diff = diff%26;
134+
}
135+
shftedcharCode = 122-diff;
136+
result = shftedcharCode;
137+
}
138+
139+
else if ( shftedcharCode>=97 && shftedcharCode<=122 ){
140+
result = shftedcharCode;
141+
}
142+
143+
else if (shftedcharCode>122 ){
144+
let diff = Math.abs(shftedcharCode-1-122)%26;
145+
146+
while( (diff%26) >= 26){
147+
diff = diff%26;
148+
}
149+
shftedcharCode = 97+diff;
150+
result = shftedcharCode;
23151
}
152+
24153
}
154+
outStr = outStr + String.fromCharCode(parseInt(result));
25155
}
26-
return key;
156+
return outStr;
27157
}

0 commit comments

Comments
 (0)