Skip to content

Commit 9cabd46

Browse files
authored
Merge pull request #78 from winsonrich/patch-4
Create keyFinder.js
2 parents 7b82729 + 7a04d2a commit 9cabd46

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

Ciphers/keyFinder.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/******************************************************
2+
Find and retrieve the encryption key automatically
3+
Note: This is a draft version, please help to modify, Thanks!
4+
******************************************************/
5+
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;
23+
}
24+
}
25+
}
26+
return key;
27+
}

0 commit comments

Comments
 (0)