|
1 |
| -/****************************************************** |
2 |
| - Find and retrieve the encryption key automatically |
3 |
| - Note: This is a draft version, please help to modify, Thanks! |
4 |
| - ******************************************************/ |
| 1 | +/** |
| 2 | + * Find and retrieve the encryption key automatically. |
| 3 | + *@param {string} str - The input encrypted string. |
| 4 | + *@returns {number} - The encryption key found, or 0 if not found. |
| 5 | + */ |
5 | 6 | functionkeyFinder(str){
|
6 | 7 | // str is used to get the input of encrypted string
|
7 | 8 | constwordBank=[
|
@@ -30,8 +31,6 @@ function keyFinder(str) {
|
30 | 31 | ' be ',
|
31 | 32 | 'Be '
|
32 | 33 | ]
|
33 |
| -// let wordbankelementCounter = 0; |
34 |
| -// let key = 0; // return zero means the key can not be found |
35 | 34 | constinStr=str.toString()// convert the input to String
|
36 | 35 | letoutStr=''// store the output value
|
37 | 36 | letoutStrElement=''// temporary store the word inside the outStr, it is used for comparison
|
@@ -59,89 +58,95 @@ function keyFinder(str) {
|
59 | 58 | return0// return 0 if found nothing
|
60 | 59 | }
|
61 | 60 |
|
62 |
| -/* this sub-function is used to assist the keyFinder to find the key */ |
| 61 | +/** |
| 62 | + * This sub-function is used to assist the keyFinder in finding the key. |
| 63 | + *@param {string} inStr - The input string. |
| 64 | + *@param {number} numShifted - The number of characters to shift in the Caesar cipher. |
| 65 | + *@returns {string} - The decrypted string. |
| 66 | + */ |
63 | 67 | functioncaesarCipherEncodeAndDecodeEngine(inStr,numShifted){
|
64 | 68 | constshiftNum=numShifted
|
65 | 69 | letcharCode=0
|
66 |
| -letoutStr='' |
67 | 70 | letshiftedCharCode=0
|
68 | 71 | letresult=0
|
69 | 72 |
|
70 |
| -for(leti=0;i<inStr.length;i++){ |
71 |
| -charCode=inStr[i].charCodeAt() |
72 |
| -shiftedCharCode=charCode+shiftNum |
73 |
| -result=charCode |
| 73 | +returninStr |
| 74 | +.split('') |
| 75 | +.map((char)=>{ |
| 76 | +charCode=char.charCodeAt() |
| 77 | +shiftedCharCode=charCode+shiftNum |
| 78 | +result=charCode |
74 | 79 |
|
75 |
| -if(charCode>=48&&charCode<=57){ |
76 |
| -if(shiftedCharCode<48){ |
77 |
| -letdiff=Math.abs(48-1-shiftedCharCode)%10 |
| 80 | +if(charCode>=48&&charCode<=57){ |
| 81 | +if(shiftedCharCode<48){ |
| 82 | +letdiff=Math.abs(48-1-shiftedCharCode)%10 |
78 | 83 |
|
79 |
| -while(diff>=10){ |
80 |
| -diff=diff%10 |
81 |
| -} |
82 |
| -document.getElementById('diffID').innerHTML=diff |
| 84 | +while(diff>=10){ |
| 85 | +diff=diff%10 |
| 86 | +} |
| 87 | +document.getElementById('diffID').innerHTML=diff |
83 | 88 |
|
84 |
| -shiftedCharCode=57-diff |
| 89 | +shiftedCharCode=57-diff |
85 | 90 |
|
86 |
| -result=shiftedCharCode |
87 |
| -}elseif(shiftedCharCode>=48&&shiftedCharCode<=57){ |
88 |
| -result=shiftedCharCode |
89 |
| -}elseif(shiftedCharCode>57){ |
90 |
| -letdiff=Math.abs(57+1-shiftedCharCode)%10 |
| 91 | +result=shiftedCharCode |
| 92 | +}elseif(shiftedCharCode>=48&&shiftedCharCode<=57){ |
| 93 | +result=shiftedCharCode |
| 94 | +}elseif(shiftedCharCode>57){ |
| 95 | +letdiff=Math.abs(57+1-shiftedCharCode)%10 |
91 | 96 |
|
92 |
| -while(diff>=10){ |
93 |
| -diff=diff%10 |
94 |
| -} |
95 |
| -document.getElementById('diffID').innerHTML=diff |
| 97 | +while(diff>=10){ |
| 98 | +diff=diff%10 |
| 99 | +} |
| 100 | +document.getElementById('diffID').innerHTML=diff |
96 | 101 |
|
97 |
| -shiftedCharCode=48+diff |
| 102 | +shiftedCharCode=48+diff |
98 | 103 |
|
99 |
| -result=shiftedCharCode |
100 |
| -} |
101 |
| -}elseif(charCode>=65&&charCode<=90){ |
102 |
| -if(shiftedCharCode<=64){ |
103 |
| -letdiff=Math.abs(65-1-shiftedCharCode)%26 |
104 |
| - |
105 |
| -while(diff%26>=26){ |
106 |
| -diff=diff%26 |
| 104 | +result=shiftedCharCode |
107 | 105 | }
|
108 |
| -shiftedCharCode=90-diff |
109 |
| -result=shiftedCharCode |
110 |
| -}elseif(shiftedCharCode>=65&&shiftedCharCode<=90){ |
111 |
| -result=shiftedCharCode |
112 |
| -}elseif(shiftedCharCode>90){ |
113 |
| -letdiff=Math.abs(shiftedCharCode-1-90)%26 |
114 |
| - |
115 |
| -while(diff%26>=26){ |
116 |
| -diff=diff%26 |
| 106 | +}elseif(charCode>=65&&charCode<=90){ |
| 107 | +if(shiftedCharCode<=64){ |
| 108 | +letdiff=Math.abs(65-1-shiftedCharCode)%26 |
| 109 | + |
| 110 | +while(diff%26>=26){ |
| 111 | +diff=diff%26 |
| 112 | +} |
| 113 | +shiftedCharCode=90-diff |
| 114 | +result=shiftedCharCode |
| 115 | +}elseif(shiftedCharCode>=65&&shiftedCharCode<=90){ |
| 116 | +result=shiftedCharCode |
| 117 | +}elseif(shiftedCharCode>90){ |
| 118 | +letdiff=Math.abs(shiftedCharCode-1-90)%26 |
| 119 | + |
| 120 | +while(diff%26>=26){ |
| 121 | +diff=diff%26 |
| 122 | +} |
| 123 | +shiftedCharCode=65+diff |
| 124 | +result=shiftedCharCode |
117 | 125 | }
|
118 |
| -shiftedCharCode=65+diff |
119 |
| -result=shiftedCharCode |
120 |
| -} |
121 |
| -}elseif(charCode>=97&&charCode<=122){ |
122 |
| -if(shiftedCharCode<=96){ |
123 |
| -letdiff=Math.abs(97-1-shiftedCharCode)%26 |
124 |
| - |
125 |
| -while(diff%26>=26){ |
126 |
| -diff=diff%26 |
127 |
| -} |
128 |
| -shiftedCharCode=122-diff |
129 |
| -result=shiftedCharCode |
130 |
| -}elseif(shiftedCharCode>=97&&shiftedCharCode<=122){ |
131 |
| -result=shiftedCharCode |
132 |
| -}elseif(shiftedCharCode>122){ |
133 |
| -letdiff=Math.abs(shiftedCharCode-1-122)%26 |
134 |
| - |
135 |
| -while(diff%26>=26){ |
136 |
| -diff=diff%26 |
| 126 | +}elseif(charCode>=97&&charCode<=122){ |
| 127 | +if(shiftedCharCode<=96){ |
| 128 | +letdiff=Math.abs(97-1-shiftedCharCode)%26 |
| 129 | + |
| 130 | +while(diff%26>=26){ |
| 131 | +diff=diff%26 |
| 132 | +} |
| 133 | +shiftedCharCode=122-diff |
| 134 | +result=shiftedCharCode |
| 135 | +}elseif(shiftedCharCode>=97&&shiftedCharCode<=122){ |
| 136 | +result=shiftedCharCode |
| 137 | +}elseif(shiftedCharCode>122){ |
| 138 | +letdiff=Math.abs(shiftedCharCode-1-122)%26 |
| 139 | + |
| 140 | +while(diff%26>=26){ |
| 141 | +diff=diff%26 |
| 142 | +} |
| 143 | +shiftedCharCode=97+diff |
| 144 | +result=shiftedCharCode |
137 | 145 | }
|
138 |
| -shiftedCharCode=97+diff |
139 |
| -result=shiftedCharCode |
140 | 146 | }
|
141 |
| -} |
142 |
| -outStr=outStr+String.fromCharCode(parseInt(result)) |
143 |
| -} |
144 |
| -returnoutStr |
| 147 | +returnString.fromCharCode(parseInt(result)) |
| 148 | +}) |
| 149 | +.join('') |
145 | 150 | }
|
146 | 151 |
|
147 | 152 | export{keyFinder}
|
|