Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd7dc852

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 <IcarusTheFly@users.noreply.github.com>Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parentf8ffacd commitd7dc852

File tree

2 files changed

+79
-72
lines changed

2 files changed

+79
-72
lines changed

‎Ciphers/KeyFinder.js

Lines changed: 77 additions & 72 deletions
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
functionkeyFinder(str){
67
// str is used to get the input of encrypted string
78
constwordBank=[
@@ -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
constinStr=str.toString()// convert the input to String
3635
letoutStr=''// store the output value
3736
letoutStrElement=''// temporary store the word inside the outStr, it is used for comparison
@@ -59,89 +58,95 @@ function keyFinder(str) {
5958
return0// 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
functioncaesarCipherEncodeAndDecodeEngine(inStr,numShifted){
6468
constshiftNum=numShifted
6569
letcharCode=0
66-
letoutStr=''
6770
letshiftedCharCode=0
6871
letresult=0
6972

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
7479

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
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-
}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
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-
}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
107105
}
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
117125
}
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
137145
}
138-
shiftedCharCode=97+diff
139-
result=shiftedCharCode
140146
}
141-
}
142-
outStr=outStr+String.fromCharCode(parseInt(result))
143-
}
144-
returnoutStr
147+
returnString.fromCharCode(parseInt(result))
148+
})
149+
.join('')
145150
}
146151

147152
export{keyFinder}

‎DIRECTORY.md

Lines changed: 2 additions & 0 deletions
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)

[8]ページ先頭

©2009-2025 Movatter.jp