feat: Key finder improvement (#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 <IcarusTheFly@users.noreply.github.com>
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
This commit is contained in:
Omar Ferreiro
2023-10-10 08:59:51 +02:00
committed by GitHub
parent f8ffacd481
commit d7dc85232c
2 changed files with 81 additions and 74 deletions

View File

@ -1,7 +1,8 @@
/****************************************************** /**
Find and retrieve the encryption key automatically * Find and retrieve the encryption key automatically.
Note: This is a draft version, please help to modify, Thanks! * @param {string} str - The input encrypted string.
******************************************************/ * @returns {number} - The encryption key found, or 0 if not found.
*/
function keyFinder(str) { function keyFinder(str) {
// str is used to get the input of encrypted string // str is used to get the input of encrypted string
const wordBank = [ const wordBank = [
@ -30,8 +31,6 @@ function keyFinder(str) {
' be ', ' be ',
'Be ' 'Be '
] ]
// let wordbankelementCounter = 0;
// let key = 0; // return zero means the key can not be found
const inStr = str.toString() // convert the input to String const inStr = str.toString() // convert the input to String
let outStr = '' // store the output value let outStr = '' // store the output value
let outStrElement = '' // temporary store the word inside the outStr, it is used for comparison let outStrElement = '' // temporary store the word inside the outStr, it is used for comparison
@ -59,16 +58,22 @@ function keyFinder(str) {
return 0 // return 0 if found nothing return 0 // return 0 if found nothing
} }
/* this sub-function is used to assist the keyFinder to find the key */ /**
* This sub-function is used to assist the keyFinder in finding the key.
* @param {string} inStr - The input string.
* @param {number} numShifted - The number of characters to shift in the Caesar cipher.
* @returns {string} - The decrypted string.
*/
function caesarCipherEncodeAndDecodeEngine(inStr, numShifted) { function caesarCipherEncodeAndDecodeEngine(inStr, numShifted) {
const shiftNum = numShifted const shiftNum = numShifted
let charCode = 0 let charCode = 0
let outStr = ''
let shiftedCharCode = 0 let shiftedCharCode = 0
let result = 0 let result = 0
for (let i = 0; i < inStr.length; i++) { return inStr
charCode = inStr[i].charCodeAt() .split('')
.map((char) => {
charCode = char.charCodeAt()
shiftedCharCode = charCode + shiftNum shiftedCharCode = charCode + shiftNum
result = charCode result = charCode
@ -139,9 +144,9 @@ function caesarCipherEncodeAndDecodeEngine(inStr, numShifted) {
result = shiftedCharCode result = shiftedCharCode
} }
} }
outStr = outStr + String.fromCharCode(parseInt(result)) return String.fromCharCode(parseInt(result))
} })
return outStr .join('')
} }
export { keyFinder } export { keyFinder }

View File

@ -179,6 +179,7 @@
* [DecimalExpansion](Maths/DecimalExpansion.js) * [DecimalExpansion](Maths/DecimalExpansion.js)
* [DecimalIsolate](Maths/DecimalIsolate.js) * [DecimalIsolate](Maths/DecimalIsolate.js)
* [DegreeToRadian](Maths/DegreeToRadian.js) * [DegreeToRadian](Maths/DegreeToRadian.js)
* [EuclideanDistance](Maths/EuclideanDistance.js)
* [EulerMethod](Maths/EulerMethod.js) * [EulerMethod](Maths/EulerMethod.js)
* [EulersTotient](Maths/EulersTotient.js) * [EulersTotient](Maths/EulersTotient.js)
* [EulersTotientFunction](Maths/EulersTotientFunction.js) * [EulersTotientFunction](Maths/EulersTotientFunction.js)
@ -249,6 +250,7 @@
* [SumOfDigits](Maths/SumOfDigits.js) * [SumOfDigits](Maths/SumOfDigits.js)
* [SumOfGeometricProgression](Maths/SumOfGeometricProgression.js) * [SumOfGeometricProgression](Maths/SumOfGeometricProgression.js)
* [TwinPrime](Maths/TwinPrime.js) * [TwinPrime](Maths/TwinPrime.js)
* [TwoSum](Maths/TwoSum.js)
* [Volume](Maths/Volume.js) * [Volume](Maths/Volume.js)
* [WhileLoopFactorial](Maths/WhileLoopFactorial.js) * [WhileLoopFactorial](Maths/WhileLoopFactorial.js)
* [ZellersCongruenceAlgorithm](Maths/ZellersCongruenceAlgorithm.js) * [ZellersCongruenceAlgorithm](Maths/ZellersCongruenceAlgorithm.js)