mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-20 18:43:43 +08:00
npx standard --fix
This commit is contained in:
@ -1,157 +1,122 @@
|
||||
/******************************************************
|
||||
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!
|
||||
******************************************************/
|
||||
function keyFinder(str){ // str is used to get the input of encrypted string
|
||||
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 "];
|
||||
//let wordbankelementCounter = 0;
|
||||
//let key = 0; // return zero means the key can not be found
|
||||
let inStr = str.toString(); //convert the input to String
|
||||
let outStr = ""; // store the output value
|
||||
let outStrElement = ""; // temporary store the word inside the outStr, it is used for comparison
|
||||
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
|
||||
outStr = caesarCipherEncodeAndDecodeEngine(inStr,k); // use the encrytpion engine to decrypt the input string
|
||||
function keyFinder (str) { // str is used to get the input of encrypted string
|
||||
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 ']
|
||||
// let wordbankelementCounter = 0;
|
||||
// let key = 0; // return zero means the key can not be found
|
||||
const inStr = str.toString() // convert the input to String
|
||||
let outStr = '' // store the output value
|
||||
let outStrElement = '' // temporary store the word inside the outStr, it is used for comparison
|
||||
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
|
||||
outStr = caesarCipherEncodeAndDecodeEngine(inStr, k) // use the encrytpion engine to decrypt the input string
|
||||
|
||||
//loop through the whole input string
|
||||
for ( let s=0; s < outStr.length; s++){
|
||||
// loop through the whole input string
|
||||
for (let s = 0; s < outStr.length; s++) {
|
||||
for (let i = 0; i < wordbank.length; i++) {
|
||||
// initialize the outStrElement which is a temp output string for comparison,
|
||||
// use a loop to find the next digit of wordbank element and compare with outStr's digit
|
||||
for (let w = 0; w < wordbank[i].length; w++) {
|
||||
outStrElement += outStr[s + w]
|
||||
}
|
||||
|
||||
for ( let i=0; i < wordbank.length; i++){
|
||||
// console.log( k + outStrElement + wordbank[i] );//debug
|
||||
|
||||
// initialize the outStrElement which is a temp output string for comparison,
|
||||
// use a loop to find the next digit of wordbank element and compare with outStr's digit
|
||||
for ( let w=0; w < wordbank[i].length; w++){
|
||||
outStrElement += outStr[ s + w ];
|
||||
}
|
||||
// this part need to be optimize with the calculation of the number of occurance of word's probabilities
|
||||
// linked list will be used in the next stage of development to calculate the number of occurace of the key
|
||||
if (wordbank[i] == outStrElement) {
|
||||
return k // return the key number if founded
|
||||
}
|
||||
|
||||
//console.log( k + outStrElement + wordbank[i] );//debug
|
||||
|
||||
// this part need to be optimize with the calculation of the number of occurance of word's probabilities
|
||||
// linked list will be used in the next stage of development to calculate the number of occurace of the key
|
||||
if (wordbank[i] == outStrElement){
|
||||
return k; // return the key number if founded
|
||||
}
|
||||
|
||||
outStrElement = ""; //reset the temp word
|
||||
|
||||
} // end for ( let i=0; i < wordbank.length; i++)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
return 0; // return 0 if found nothing
|
||||
outStrElement = '' // reset the temp word
|
||||
} // end for ( let i=0; i < wordbank.length; i++)
|
||||
}
|
||||
}
|
||||
return 0 // return 0 if found nothing
|
||||
}
|
||||
|
||||
/* this sub-function is used to assist the keyfinder to find the key */
|
||||
function caesarCipherEncodeAndDecodeEngine(inStr, numShifted)
|
||||
{
|
||||
let shiftNum = numShifted;
|
||||
let charCode = 0;
|
||||
let outStr = "";
|
||||
let shftedcharCode = 0;
|
||||
let result = 0;
|
||||
function caesarCipherEncodeAndDecodeEngine (inStr, numShifted) {
|
||||
const shiftNum = numShifted
|
||||
let charCode = 0
|
||||
let outStr = ''
|
||||
let shftedcharCode = 0
|
||||
let result = 0
|
||||
|
||||
for (let i=0; i<inStr.length; i++){
|
||||
for (let i = 0; i < inStr.length; i++) {
|
||||
charCode = inStr[i].charCodeAt()
|
||||
shftedcharCode = charCode + shiftNum
|
||||
result = charCode
|
||||
|
||||
charCode = inStr[i].charCodeAt();
|
||||
shftedcharCode = charCode + shiftNum;
|
||||
result = charCode;
|
||||
if ((charCode >= 48 && charCode <= 57)) {
|
||||
if (shftedcharCode < 48) {
|
||||
let diff = Math.abs(48 - 1 - shftedcharCode) % 10
|
||||
|
||||
if ( (charCode>=48 && charCode<=57))
|
||||
{
|
||||
if ( shftedcharCode < 48 ){
|
||||
while (diff >= 10) {
|
||||
diff = diff % 10
|
||||
}
|
||||
document.getElementById('diffID').innerHTML = diff
|
||||
|
||||
let diff = Math.abs(48-1-shftedcharCode)%10;
|
||||
shftedcharCode = 57 - diff
|
||||
|
||||
while( diff >= 10){
|
||||
diff = diff%10;
|
||||
}
|
||||
document.getElementById("diffID").innerHTML = diff;
|
||||
result = shftedcharCode
|
||||
} else if (shftedcharCode >= 48 && shftedcharCode <= 57) {
|
||||
result = shftedcharCode
|
||||
} else if (shftedcharCode > 57) {
|
||||
let diff = Math.abs(57 + 1 - shftedcharCode) % 10
|
||||
|
||||
shftedcharCode = 57-diff;
|
||||
|
||||
result = shftedcharCode;
|
||||
}
|
||||
while (diff >= 10) {
|
||||
diff = diff % 10
|
||||
}
|
||||
document.getElementById('diffID').innerHTML = diff
|
||||
|
||||
else if ( shftedcharCode>=48 && shftedcharCode<=57 ){
|
||||
result = shftedcharCode;
|
||||
}
|
||||
shftedcharCode = 48 + diff
|
||||
|
||||
else if ( shftedcharCode > 57 ){
|
||||
result = shftedcharCode
|
||||
}
|
||||
} else if ((charCode >= 65 && charCode <= 90)) {
|
||||
if (shftedcharCode <= 64) {
|
||||
let diff = Math.abs(65 - 1 - shftedcharCode) % 26
|
||||
|
||||
let diff = Math.abs(57+1-shftedcharCode)%10;
|
||||
while ((diff % 26) >= 26) {
|
||||
diff = diff % 26
|
||||
}
|
||||
shftedcharCode = 90 - diff
|
||||
result = shftedcharCode
|
||||
} else if (shftedcharCode >= 65 && shftedcharCode <= 90) {
|
||||
result = shftedcharCode
|
||||
} else if (shftedcharCode > 90) {
|
||||
let diff = Math.abs(shftedcharCode - 1 - 90) % 26
|
||||
|
||||
while( diff >= 10){
|
||||
diff = diff%10;
|
||||
}
|
||||
document.getElementById("diffID").innerHTML = diff;
|
||||
while ((diff % 26) >= 26) {
|
||||
diff = diff % 26
|
||||
}
|
||||
shftedcharCode = 65 + diff
|
||||
result = shftedcharCode
|
||||
}
|
||||
} else if ((charCode >= 97 && charCode <= 122)) {
|
||||
if (shftedcharCode <= 96) {
|
||||
let diff = Math.abs(97 - 1 - shftedcharCode) % 26
|
||||
|
||||
shftedcharCode = 48+diff;
|
||||
while ((diff % 26) >= 26) {
|
||||
diff = diff % 26
|
||||
}
|
||||
shftedcharCode = 122 - diff
|
||||
result = shftedcharCode
|
||||
} else if (shftedcharCode >= 97 && shftedcharCode <= 122) {
|
||||
result = shftedcharCode
|
||||
} else if (shftedcharCode > 122) {
|
||||
let diff = Math.abs(shftedcharCode - 1 - 122) % 26
|
||||
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
else if ( (charCode>=65 && charCode<=90) )
|
||||
{
|
||||
|
||||
if (shftedcharCode <=64 ){
|
||||
|
||||
let diff = Math.abs(65-1-shftedcharCode)%26;
|
||||
|
||||
while( (diff%26) >= 26){
|
||||
diff = diff%26;
|
||||
}
|
||||
shftedcharCode = 90-diff;
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
else if ( shftedcharCode>=65 && shftedcharCode<=90 ){
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
else if (shftedcharCode>90 ){
|
||||
let diff = Math.abs(shftedcharCode-1-90)%26;
|
||||
|
||||
while( (diff%26) >= 26){
|
||||
diff = diff%26;
|
||||
}
|
||||
shftedcharCode = 65+diff;
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
else if ( (charCode>=97 && charCode<=122))
|
||||
{
|
||||
if ( shftedcharCode<=96 ){
|
||||
|
||||
let diff = Math.abs(97-1-shftedcharCode)%26;
|
||||
|
||||
while( (diff%26) >= 26){
|
||||
diff = diff%26;
|
||||
}
|
||||
shftedcharCode = 122-diff;
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
else if ( shftedcharCode>=97 && shftedcharCode<=122 ){
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
else if (shftedcharCode>122 ){
|
||||
let diff = Math.abs(shftedcharCode-1-122)%26;
|
||||
|
||||
while( (diff%26) >= 26){
|
||||
diff = diff%26;
|
||||
}
|
||||
shftedcharCode = 97+diff;
|
||||
result = shftedcharCode;
|
||||
}
|
||||
|
||||
}
|
||||
outStr = outStr + String.fromCharCode(parseInt(result));
|
||||
}
|
||||
return outStr;
|
||||
while ((diff % 26) >= 26) {
|
||||
diff = diff % 26
|
||||
}
|
||||
shftedcharCode = 97 + diff
|
||||
result = shftedcharCode
|
||||
}
|
||||
}
|
||||
outStr = outStr + String.fromCharCode(parseInt(result))
|
||||
}
|
||||
return outStr
|
||||
}
|
||||
|
Reference in New Issue
Block a user