From 6c894a2aa5322d30d5f8fb90730e96f3399bf1b2 Mon Sep 17 00:00:00 2001 From: Wan Cheuk Lun Date: Wed, 31 Oct 2018 09:47:06 +0800 Subject: [PATCH 1/5] Update keyFinder.js modified used the suitable identifier for the variables --- Ciphers/keyFinder.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/Ciphers/keyFinder.js b/Ciphers/keyFinder.js index 8dda33c3e..cb4160c45 100644 --- a/Ciphers/keyFinder.js +++ b/Ciphers/keyFinder.js @@ -3,23 +3,24 @@ 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 - var key = 0; // return zero means the key can not be found - var wordbank =["is","Is","am","Am","are","Are","have","Have","has","Has","may","May","be","Be"]; + const wordbank =["is","Is","am","Am","are","Are","have","Have","has","Has","may","May","be","Be"]; + let key = 0; // return zero means the key can not be found //var shiftNum = 0; //count the number of key shifted - var inStr = str.toString(); //convert the input to String - var outStr = ""; // store the output value - var wordInOutStr = ""; // temporary store the word inside the outStr, it is used for comparison + let inStr = str.toString(); //convert the input to String + let outStr = ""; // store the output value + let wordInOutStr = ""; // temporary store the word inside the outStr, it is used for comparison //document.getElementById("debug").innerHTML = shiftNum; // debug: display the shifted number(s) - for (var i=0; i<(52); i++){ //try the number of key shifted, the sum of character from a-z and A-Z is 26*2=52 + for (let i=0; i<(52); i++){ //try the number of key shifted, the sum of character from a-z and A-Z is 26*2=52 outStr = caesarCipherEncodeAndDecodeEngine(inStr,i); // use the encrytpion engine to decrypt the input string, shiftNum=i - for ( var i=0; i Date: Wed, 31 Oct 2018 10:10:17 +0800 Subject: [PATCH 2/5] Update keyFinder.js 1. modified the identifiers and used the suitable identifiers for the variables 2. leave the loop when a key is match and found to increase the speed of searching in this stage of development 3. return the key number if founded, return 0 if found nothing --- Ciphers/keyFinder.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/Ciphers/keyFinder.js b/Ciphers/keyFinder.js index cb4160c45..b223096c3 100644 --- a/Ciphers/keyFinder.js +++ b/Ciphers/keyFinder.js @@ -5,11 +5,9 @@ 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 =["is","Is","am","Am","are","Are","have","Have","has","Has","may","May","be","Be"]; let key = 0; // return zero means the key can not be found - //var shiftNum = 0; //count the number of key shifted let inStr = str.toString(); //convert the input to String let outStr = ""; // store the output value let wordInOutStr = ""; // temporary store the word inside the outStr, it is used for comparison - //document.getElementById("debug").innerHTML = shiftNum; // debug: display the shifted number(s) for (let i=0; i<(52); i++){ //try the number of key shifted, the sum of character from a-z and A-Z is 26*2=52 outStr = caesarCipherEncodeAndDecodeEngine(inStr,i); // use the encrytpion engine to decrypt the input string, shiftNum=i for ( let i=0; i Date: Wed, 31 Oct 2018 10:36:52 +0800 Subject: [PATCH 3/5] Update keyFinder.js a sub-function is used to assist the keyfinder to find the key --- Ciphers/keyFinder.js | 115 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) diff --git a/Ciphers/keyFinder.js b/Ciphers/keyFinder.js index b223096c3..8e16dedb5 100644 --- a/Ciphers/keyFinder.js +++ b/Ciphers/keyFinder.js @@ -24,3 +24,118 @@ function keyFinder(str){ // str is used to get the input of encrypted string } 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; + + for (let i=0; i=48 && charCode<=57)) + { + if ( shftedcharCode < 48 ){ + + let diff = Math.abs(48-1-shftedcharCode)%10; + + while( diff >= 10){ + diff = diff%10; + } + document.getElementById("diffID").innerHTML = diff; + + shftedcharCode = 57-diff; + + result = shftedcharCode; + } + + else if ( shftedcharCode>=48 && shftedcharCode<=57 ){ + result = shftedcharCode; + } + + else if ( shftedcharCode > 57 ){ + + let diff = Math.abs(57+1-shftedcharCode)%10; + + while( diff >= 10){ + diff = diff%10; + } + document.getElementById("diffID").innerHTML = diff; + + shftedcharCode = 48+diff; + + 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; +} From 75d7ef53dc7553a274ac0ff980940794e7ba880b Mon Sep 17 00:00:00 2001 From: Wan Cheuk Lun Date: Wed, 31 Oct 2018 16:54:20 +0800 Subject: [PATCH 4/5] Update keyFinder.js Finally, there are several changes in the function keyFinder(str) that make it works well --- Ciphers/keyFinder.js | 46 +++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 15 deletions(-) diff --git a/Ciphers/keyFinder.js b/Ciphers/keyFinder.js index 8e16dedb5..4186eae0a 100644 --- a/Ciphers/keyFinder.js +++ b/Ciphers/keyFinder.js @@ -3,24 +3,40 @@ 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 =["is","Is","am","Am","are","Are","have","Have","has","Has","may","May","be","Be"]; - let key = 0; // return zero means the key can not be found + const wordbank =[" 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 wordInOutStr = ""; // temporary store the word inside the outStr, it is used for comparison - for (let i=0; i<(52); i++){ //try the number of key shifted, the sum of character from a-z and A-Z is 26*2=52 - outStr = caesarCipherEncodeAndDecodeEngine(inStr,i); // use the encrytpion engine to decrypt the input string, shiftNum=i - for ( let i=0; i Date: Wed, 31 Oct 2018 17:08:24 +0800 Subject: [PATCH 5/5] Update keyFinder.js Add some key words to the wordbank to increase the chance of the matching. --- Ciphers/keyFinder.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Ciphers/keyFinder.js b/Ciphers/keyFinder.js index 4186eae0a..f02171d49 100644 --- a/Ciphers/keyFinder.js +++ b/Ciphers/keyFinder.js @@ -3,7 +3,7 @@ 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 =[" the ","The "," of "," is ","Is "," am ","Am "," are ","Are "," have ","Have "," has ","Has "," may ","May "," be ","Be "]; + 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