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