From 633105633bec1971b527111eeef2df8f194c8946 Mon Sep 17 00:00:00 2001 From: Wan Cheuk Lun Date: Thu, 25 Oct 2018 20:15:11 +0800 Subject: [PATCH 1/5] Create keyFinder Find and retrieve the encryption key automatically Note: This is a draft version, please help to modify, Thanks! --- Ciphers/keyFinder | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Ciphers/keyFinder diff --git a/Ciphers/keyFinder b/Ciphers/keyFinder new file mode 100644 index 0000000000..5b8bd791f6 --- /dev/null +++ b/Ciphers/keyFinder @@ -0,0 +1,23 @@ +/****************************************************** +Find and retrieve the encryption key automatically +Note: This is a draft version, please help to modify, Thanks! +******************************************************/ +function keyFinder(){ + var result = ""; + var key = 0; + var wordbank =["is","Is","am","Am","are","Are","have","Have","has","Has","may","May","be","Be"]; + var shiftNum = 0; //count the number of key shifted + var str = document.getElementById("encryptedID").value; //get the input string + var inStr = str.toString(); //convert the input to String + var outStr = ""; // store the output value + document.getElementById("debug").innerHTML = shiftNum; + for (var i=0; i<26; i++){ + outStr = caesarCipherEncodeAndDecodeEngine(inStr,i); // use the encrytpion engine to decrypt the input string, shiftNum=i + for ( var i=0; i Date: Sat, 27 Oct 2018 10:38:07 +0800 Subject: [PATCH 2/5] Rename keyFinder to keyFinder.js --- Ciphers/{keyFinder => keyFinder.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Ciphers/{keyFinder => keyFinder.js} (100%) diff --git a/Ciphers/keyFinder b/Ciphers/keyFinder.js similarity index 100% rename from Ciphers/keyFinder rename to Ciphers/keyFinder.js From e0fa99b2fd36b131cbfe607ea15634a3fbd47c7a Mon Sep 17 00:00:00 2001 From: Wan Cheuk Lun Date: Sat, 27 Oct 2018 11:13:15 +0800 Subject: [PATCH 3/5] Update keyFinder.js optimized the indentation for the if statement in line 18 --- Ciphers/keyFinder.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Ciphers/keyFinder.js b/Ciphers/keyFinder.js index 5b8bd791f6..d0c12c2251 100644 --- a/Ciphers/keyFinder.js +++ b/Ciphers/keyFinder.js @@ -10,14 +10,14 @@ function keyFinder(){ var str = document.getElementById("encryptedID").value; //get the input string var inStr = str.toString(); //convert the input to String var outStr = ""; // store the output value - document.getElementById("debug").innerHTML = shiftNum; + document.getElementById("debug").innerHTML = shiftNum; // debug: display the shifted number(s) for (var i=0; i<26; i++){ outStr = caesarCipherEncodeAndDecodeEngine(inStr,i); // use the encrytpion engine to decrypt the input string, shiftNum=i for ( var i=0; i Date: Sat, 27 Oct 2018 12:29:36 +0800 Subject: [PATCH 4/5] Update keyFinder.js some changes is made to optimize the function --- Ciphers/keyFinder.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/Ciphers/keyFinder.js b/Ciphers/keyFinder.js index d0c12c2251..30b02097e4 100644 --- a/Ciphers/keyFinder.js +++ b/Ciphers/keyFinder.js @@ -2,16 +2,14 @@ Find and retrieve the encryption key automatically Note: This is a draft version, please help to modify, Thanks! ******************************************************/ -function keyFinder(){ - var result = ""; - var key = 0; +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"]; - var shiftNum = 0; //count the number of key shifted - var str = document.getElementById("encryptedID").value; //get the input string + //var shiftNum = 0; //count the number of key shifted var inStr = str.toString(); //convert the input to String var outStr = ""; // store the output value - document.getElementById("debug").innerHTML = shiftNum; // debug: display the shifted number(s) - for (var i=0; i<26; i++){ + //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 outStr = caesarCipherEncodeAndDecodeEngine(inStr,i); // use the encrytpion engine to decrypt the input string, shiftNum=i for ( var i=0; i Date: Tue, 30 Oct 2018 11:20:32 +0800 Subject: [PATCH 5/5] Update keyFinder.js Made 2 changes to optimize the match results: 1. use a loop to find the next digit of wordbank element and compare with outStr's digit 2. this part need to be optimize with the calculation of the number of occurance of word's probabilities --- Ciphers/keyFinder.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Ciphers/keyFinder.js b/Ciphers/keyFinder.js index 30b02097e4..8dda33c3e7 100644 --- a/Ciphers/keyFinder.js +++ b/Ciphers/keyFinder.js @@ -8,11 +8,17 @@ function keyFinder(str){ // str is used to get the input of encrypted string //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 //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 outStr = caesarCipherEncodeAndDecodeEngine(inStr,i); // use the encrytpion engine to decrypt the input string, shiftNum=i for ( var i=0; i