From 5883f3d9aeecb1ab5754f3cf8dcb617a5f9a6243 Mon Sep 17 00:00:00 2001 From: LethargicLeprechaun Date: Sat, 2 May 2020 16:55:51 +0100 Subject: [PATCH 1/3] Added an XOR cipher --- Ciphers/xor_cipher.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Ciphers/xor_cipher.js diff --git a/Ciphers/xor_cipher.js b/Ciphers/xor_cipher.js new file mode 100644 index 0000000000..2d778647fe --- /dev/null +++ b/Ciphers/xor_cipher.js @@ -0,0 +1,25 @@ +/** + * The XOR cipher is a type of additive cipher. + * Each character is bitwise XORed with the key. + * We loop through the input string, XORing each + * character with the key. + */ + +/** + * Encrypt using an XOR cipher + * @param {String} str - String to be encrypted + * @param {Number} key - key for encryption + * @return {String} encrypted string + */ +function XOR(str, key) { + let result = ""; + for (const elem of str) { + result += String.fromCharCode(elem.charCodeAt(0) ^ key); + } + return result; +} + +const encryptedString = XOR("test string", 32); +console.log("Encrypted: ", encryptedString); +const decryptedString = XOR(encryptedString, 32); +console.log("Decrypted: ", decryptedString); From d10bf63889dfe5152c84eac7424a71cdbe2cffbe Mon Sep 17 00:00:00 2001 From: vinayak Date: Tue, 23 Jun 2020 10:07:44 +0530 Subject: [PATCH 2/3] Rename xor_cipher.js to XORCipher.js --- Ciphers/{xor_cipher.js => XORCipher.js} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename Ciphers/{xor_cipher.js => XORCipher.js} (100%) diff --git a/Ciphers/xor_cipher.js b/Ciphers/XORCipher.js similarity index 100% rename from Ciphers/xor_cipher.js rename to Ciphers/XORCipher.js From 955e083af1e2f99222f0c110f312f1e2d2728eb5 Mon Sep 17 00:00:00 2001 From: vinayak Date: Tue, 23 Jun 2020 10:13:12 +0530 Subject: [PATCH 3/3] Update XORCipher.js --- Ciphers/XORCipher.js | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/Ciphers/XORCipher.js b/Ciphers/XORCipher.js index 2d778647fe..898b200dd6 100644 --- a/Ciphers/XORCipher.js +++ b/Ciphers/XORCipher.js @@ -11,15 +11,16 @@ * @param {Number} key - key for encryption * @return {String} encrypted string */ -function XOR(str, key) { - let result = ""; - for (const elem of str) { - result += String.fromCharCode(elem.charCodeAt(0) ^ key); - } - return result; + +function XOR (str, key) { + let result = '' + for (const elem of str) { + result += String.fromCharCode(elem.charCodeAt(0) ^ key) + } + return result } -const encryptedString = XOR("test string", 32); -console.log("Encrypted: ", encryptedString); -const decryptedString = XOR(encryptedString, 32); -console.log("Decrypted: ", decryptedString); +const encryptedString = XOR('test string', 32) +console.log('Encrypted: ', encryptedString) +const decryptedString = XOR(encryptedString, 32) +console.log('Decrypted: ', decryptedString)