Skip to content

Commit d7dc852

Browse files
IcarusTheFlygithub-actions
and
github-actions
authored
feat: Key finder improvement (TheAlgorithms#1456)
* Improve algorithm * Updated Documentation in README.md * Updated Documentation in README.md * Remove unwanted changes * Make the changes fit * Updated Documentation in README.md --------- Co-authored-by: IcarusTheFly <[email protected]> Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent f8ffacd commit d7dc852

File tree

2 files changed

+79
-72
lines changed

2 files changed

+79
-72
lines changed

Ciphers/KeyFinder.js

+77-72
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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+
*/
56
function keyFinder(str) {
67
// str is used to get the input of encrypted string
78
const wordBank = [
@@ -30,8 +31,6 @@ function keyFinder(str) {
3031
' be ',
3132
'Be '
3233
]
33-
// let wordbankelementCounter = 0;
34-
// let key = 0; // return zero means the key can not be found
3534
const inStr = str.toString() // convert the input to String
3635
let outStr = '' // store the output value
3736
let outStrElement = '' // temporary store the word inside the outStr, it is used for comparison
@@ -59,89 +58,95 @@ function keyFinder(str) {
5958
return 0 // return 0 if found nothing
6059
}
6160

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+
*/
6367
function caesarCipherEncodeAndDecodeEngine(inStr, numShifted) {
6468
const shiftNum = numShifted
6569
let charCode = 0
66-
let outStr = ''
6770
let shiftedCharCode = 0
6871
let result = 0
6972

70-
for (let i = 0; i < inStr.length; i++) {
71-
charCode = inStr[i].charCodeAt()
72-
shiftedCharCode = charCode + shiftNum
73-
result = charCode
73+
return inStr
74+
.split('')
75+
.map((char) => {
76+
charCode = char.charCodeAt()
77+
shiftedCharCode = charCode + shiftNum
78+
result = charCode
7479

75-
if (charCode >= 48 && charCode <= 57) {
76-
if (shiftedCharCode < 48) {
77-
let diff = Math.abs(48 - 1 - shiftedCharCode) % 10
80+
if (charCode >= 48 && charCode <= 57) {
81+
if (shiftedCharCode < 48) {
82+
let diff = Math.abs(48 - 1 - shiftedCharCode) % 10
7883

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
8388

84-
shiftedCharCode = 57 - diff
89+
shiftedCharCode = 57 - diff
8590

86-
result = shiftedCharCode
87-
} else if (shiftedCharCode >= 48 && shiftedCharCode <= 57) {
88-
result = shiftedCharCode
89-
} else if (shiftedCharCode > 57) {
90-
let diff = Math.abs(57 + 1 - shiftedCharCode) % 10
91+
result = shiftedCharCode
92+
} else if (shiftedCharCode >= 48 && shiftedCharCode <= 57) {
93+
result = shiftedCharCode
94+
} else if (shiftedCharCode > 57) {
95+
let diff = Math.abs(57 + 1 - shiftedCharCode) % 10
9196

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
96101

97-
shiftedCharCode = 48 + diff
102+
shiftedCharCode = 48 + diff
98103

99-
result = shiftedCharCode
100-
}
101-
} else if (charCode >= 65 && charCode <= 90) {
102-
if (shiftedCharCode <= 64) {
103-
let diff = Math.abs(65 - 1 - shiftedCharCode) % 26
104-
105-
while (diff % 26 >= 26) {
106-
diff = diff % 26
104+
result = shiftedCharCode
107105
}
108-
shiftedCharCode = 90 - diff
109-
result = shiftedCharCode
110-
} else if (shiftedCharCode >= 65 && shiftedCharCode <= 90) {
111-
result = shiftedCharCode
112-
} else if (shiftedCharCode > 90) {
113-
let diff = Math.abs(shiftedCharCode - 1 - 90) % 26
114-
115-
while (diff % 26 >= 26) {
116-
diff = diff % 26
106+
} else if (charCode >= 65 && charCode <= 90) {
107+
if (shiftedCharCode <= 64) {
108+
let diff = 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+
} else if (shiftedCharCode >= 65 && shiftedCharCode <= 90) {
116+
result = shiftedCharCode
117+
} else if (shiftedCharCode > 90) {
118+
let diff = Math.abs(shiftedCharCode - 1 - 90) % 26
119+
120+
while (diff % 26 >= 26) {
121+
diff = diff % 26
122+
}
123+
shiftedCharCode = 65 + diff
124+
result = shiftedCharCode
117125
}
118-
shiftedCharCode = 65 + diff
119-
result = shiftedCharCode
120-
}
121-
} else if (charCode >= 97 && charCode <= 122) {
122-
if (shiftedCharCode <= 96) {
123-
let diff = 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-
} else if (shiftedCharCode >= 97 && shiftedCharCode <= 122) {
131-
result = shiftedCharCode
132-
} else if (shiftedCharCode > 122) {
133-
let diff = Math.abs(shiftedCharCode - 1 - 122) % 26
134-
135-
while (diff % 26 >= 26) {
136-
diff = diff % 26
126+
} else if (charCode >= 97 && charCode <= 122) {
127+
if (shiftedCharCode <= 96) {
128+
let diff = 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+
} else if (shiftedCharCode >= 97 && shiftedCharCode <= 122) {
136+
result = shiftedCharCode
137+
} else if (shiftedCharCode > 122) {
138+
let diff = Math.abs(shiftedCharCode - 1 - 122) % 26
139+
140+
while (diff % 26 >= 26) {
141+
diff = diff % 26
142+
}
143+
shiftedCharCode = 97 + diff
144+
result = shiftedCharCode
137145
}
138-
shiftedCharCode = 97 + diff
139-
result = shiftedCharCode
140146
}
141-
}
142-
outStr = outStr + String.fromCharCode(parseInt(result))
143-
}
144-
return outStr
147+
return String.fromCharCode(parseInt(result))
148+
})
149+
.join('')
145150
}
146151

147152
export { keyFinder }

DIRECTORY.md

+2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
* [DecimalExpansion](Maths/DecimalExpansion.js)
180180
* [DecimalIsolate](Maths/DecimalIsolate.js)
181181
* [DegreeToRadian](Maths/DegreeToRadian.js)
182+
* [EuclideanDistance](Maths/EuclideanDistance.js)
182183
* [EulerMethod](Maths/EulerMethod.js)
183184
* [EulersTotient](Maths/EulersTotient.js)
184185
* [EulersTotientFunction](Maths/EulersTotientFunction.js)
@@ -249,6 +250,7 @@
249250
* [SumOfDigits](Maths/SumOfDigits.js)
250251
* [SumOfGeometricProgression](Maths/SumOfGeometricProgression.js)
251252
* [TwinPrime](Maths/TwinPrime.js)
253+
* [TwoSum](Maths/TwoSum.js)
252254
* [Volume](Maths/Volume.js)
253255
* [WhileLoopFactorial](Maths/WhileLoopFactorial.js)
254256
* [ZellersCongruenceAlgorithm](Maths/ZellersCongruenceAlgorithm.js)

0 commit comments

Comments
 (0)