@@ -3,23 +3,24 @@ Find and retrieve the encryption key automatically
33Note: This is a draft version, please help to modify, Thanks!
44******************************************************/
55function 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" ] ;
6+ const wordbank = [ "is" , "Is" , "am" , "Am" , "are" , "Are" , "have" , "Have" , "has" , "Has" , "may" , "May" , "be" , "Be" ] ;
7+ let key = 0 ; // return zero means the key can not be found
88 //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
9+ let inStr = str . toString ( ) ; //convert the input to String
10+ let outStr = "" ; // store the output value
11+ let wordInOutStr = "" ; // temporary store the word inside the outStr, it is used for comparison
1212 //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
13+ for ( let 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
1414outStr = caesarCipherEncodeAndDecodeEngine ( inStr , i ) ; // use the encrytpion engine to decrypt the input string, shiftNum=i
15- for ( var i = 0 ; i < wordbank . length ; i ++ ) {
15+ for ( let i = 0 ; i < wordbank . length ; i ++ ) {
1616// 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 ++ ) {
17+ for ( let j = 0 ; j < wordbank [ i ] . length ; j ++ ) {
1818wordInOutStr += outStr [ i + j ] ;
1919}
2020// this part need to be optimize with the calculation of the number of occurance of word's probabilities
21+ // linked list will be used in the next stage of development to calculate the number of occurace of the key
2122if ( wordbank [ i ] == wordInOutStr ) {
22- key = i ;
23+ return key = i ;
2324}
2425}
2526}
0 commit comments