@@ -3,24 +3,40 @@ 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- 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
6+ const wordbank = [ " 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
89let inStr = str . toString ( ) ; //convert the input to String
910let outStr = "" ; // store the output value
10- let wordInOutStr = "" ; // temporary store the word inside the outStr, it is used for comparison
11- 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
12- outStr = caesarCipherEncodeAndDecodeEngine ( inStr , i ) ; // use the encrytpion engine to decrypt the input string, shiftNum=i
13- for ( let i = 0 ; i < wordbank . length ; i ++ ) {
14- // use a loop to find the next digit of wordbank element and compare with outStr's digit
15- for ( let j = 0 ; j < wordbank [ i ] . length ; j ++ ) {
16- wordInOutStr += outStr [ i + j ] ;
17- }
18- // this part need to be optimize with the calculation of the number of occurance of word's probabilities
19- // linked list will be used in the next stage of development to calculate the number of occurace of the key
20- if ( wordbank [ i ] == wordInOutStr ) {
21- return key = i ; // return the key number if founded
22- }
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+
2338}
39+
2440}
2541return 0 ; // return 0 if found nothing
2642}
0 commit comments