From 27c9330b9f2362d2e1d579841d6d57e79dbee869 Mon Sep 17 00:00:00 2001 From: MANDEM_VIGNESH_KUMAR_REDDY Date: Tue, 18 Feb 2025 11:05:23 +0530 Subject: [PATCH 1/2] Create vigenere.js in these peice of code you can use it for the alphabets and including numbers and removing out all the traces of punctuation marks and special characters and making it easy to work with and along side these code has both the encryption and decryption codes --- vigenere.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 vigenere.js diff --git a/vigenere.js b/vigenere.js new file mode 100644 index 00000000..e552d982 --- /dev/null +++ b/vigenere.js @@ -0,0 +1,88 @@ +function cleanText(text) { + const textNoSpaces = text.replace(/ /g, ""); + if (/[^A-Za-z0-9]/.test(textNoSpaces)) { + throw new Error("Special characters detected. They must be removed."); + } + return textNoSpaces; +} + +function encrypt(message, key) { + message = message.toLowerCase(); + key = key.toLowerCase(); + const combinedAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789"; + const n = combinedAlphabet.length; + const encrypted = []; + let keyIndex = 0; + + try { + for (const char of message) { + if (combinedAlphabet.includes(char)) { + const mIndex = combinedAlphabet.indexOf(char); + const keyChar = key[keyIndex % key.length]; + const shift = combinedAlphabet.indexOf(keyChar); + const newIndex = (mIndex + shift) % n; + encrypted.push(combinedAlphabet[newIndex]); + keyIndex++; + } + } + return encrypted.join(""); + } catch (e) { + throw new Error("Encryption error: " + e.message); + } +} + +function decrypt(cipherText, key) { + cipherText = cipherText.toLowerCase(); + key = key.toLowerCase(); + const combinedAlphabet = "abcdefghijklmnopqrstuvwxyz0123456789"; + const n = combinedAlphabet.length; + const decrypted = []; + let keyIndex = 0; + + try { + for (const char of cipherText) { + if (combinedAlphabet.includes(char)) { + const cIndex = combinedAlphabet.indexOf(char); + const keyChar = key[keyIndex % key.length]; + const shift = combinedAlphabet.indexOf(keyChar); + const originalIndex = (cIndex - shift + n) % n; + decrypted.push(combinedAlphabet[originalIndex]); + keyIndex++; + } + } + return decrypted.join(""); + } catch (e) { + throw new Error("Decryption error: " + e.message); + } +} + +function main() { + try { + const rawMessage = prompt("Enter the message: "); + const rawKey = prompt("Enter the key: "); + try { + const cleanedMessage = cleanText(rawMessage); + const cleanedKey = cleanText(rawKey); + if (!cleanedMessage) { + throw new Error("Message is empty after cleaning!"); + } + if (!cleanedKey) { + throw new Error("Key is empty after cleaning!"); + } + const encryptedMsg = encrypt(cleanedMessage, cleanedKey); + console.log("\nEncrypted Message:"); + console.log(encryptedMsg); + const decryptedMsg = decrypt(encryptedMsg, cleanedKey); + console.log("\nDecrypted Message:"); + console.log(decryptedMsg); + } catch (ve) { + console.log("Input error:", ve.message); + return; + } + } catch (e) { + console.log("An error occurred:", e.message); + } +} + +main(); + From 4baa26336daa8c6a3565cefaf28ff5ccfb1dd807 Mon Sep 17 00:00:00 2001 From: MANDEM_VIGNESH_KUMAR_REDDY Date: Tue, 18 Feb 2025 18:30:50 +0530 Subject: [PATCH 2/2] Update vigenere.js --- vigenere.js | 97 +++++++++++++++++++++++++++-------------------------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/vigenere.js b/vigenere.js index e552d982..3a2be958 100644 --- a/vigenere.js +++ b/vigenere.js @@ -1,3 +1,9 @@ +/** + * Cleans the input text by removing spaces and ensuring it contains only alphanumeric characters. + * @param {string} text - The input text to be cleaned. + * @returns {string} The cleaned text without spaces or special characters. + * @throws Will throw an error if special characters are found. + */ function cleanText(text) { const textNoSpaces = text.replace(/ /g, ""); if (/[^A-Za-z0-9]/.test(textNoSpaces)) { @@ -6,6 +12,12 @@ function cleanText(text) { return textNoSpaces; } +/** + * Encrypts a message using a key with a substitution cipher. + * @param {string} message - The message to encrypt. + * @param {string} key - The encryption key. + * @returns {string} The encrypted message. + */ function encrypt(message, key) { message = message.toLowerCase(); key = key.toLowerCase(); @@ -14,23 +26,25 @@ function encrypt(message, key) { const encrypted = []; let keyIndex = 0; - try { - for (const char of message) { - if (combinedAlphabet.includes(char)) { - const mIndex = combinedAlphabet.indexOf(char); - const keyChar = key[keyIndex % key.length]; - const shift = combinedAlphabet.indexOf(keyChar); - const newIndex = (mIndex + shift) % n; - encrypted.push(combinedAlphabet[newIndex]); - keyIndex++; - } + for (const char of message) { + if (combinedAlphabet.includes(char)) { + const mIndex = combinedAlphabet.indexOf(char); + const keyChar = key[keyIndex % key.length]; + const shift = combinedAlphabet.indexOf(keyChar); + const newIndex = (mIndex + shift) % n; + encrypted.push(combinedAlphabet[newIndex]); + keyIndex++; } - return encrypted.join(""); - } catch (e) { - throw new Error("Encryption error: " + e.message); } + return encrypted.join(""); } +/** + * Decrypts an encrypted message using a key with a substitution cipher. + * @param {string} cipherText - The encrypted message to decrypt. + * @param {string} key - The decryption key. + * @returns {string} The decrypted message. + */ function decrypt(cipherText, key) { cipherText = cipherText.toLowerCase(); key = key.toLowerCase(); @@ -39,50 +53,39 @@ function decrypt(cipherText, key) { const decrypted = []; let keyIndex = 0; - try { - for (const char of cipherText) { - if (combinedAlphabet.includes(char)) { - const cIndex = combinedAlphabet.indexOf(char); - const keyChar = key[keyIndex % key.length]; - const shift = combinedAlphabet.indexOf(keyChar); - const originalIndex = (cIndex - shift + n) % n; - decrypted.push(combinedAlphabet[originalIndex]); - keyIndex++; - } + for (const char of cipherText) { + if (combinedAlphabet.includes(char)) { + const cIndex = combinedAlphabet.indexOf(char); + const keyChar = key[keyIndex % key.length]; + const shift = combinedAlphabet.indexOf(keyChar); + const originalIndex = (cIndex - shift + n) % n; + decrypted.push(combinedAlphabet[originalIndex]); + keyIndex++; } - return decrypted.join(""); - } catch (e) { - throw new Error("Decryption error: " + e.message); } + return decrypted.join(""); } +/** + * The main function to clean, encrypt, and decrypt messages. + */ function main() { try { const rawMessage = prompt("Enter the message: "); const rawKey = prompt("Enter the key: "); - try { - const cleanedMessage = cleanText(rawMessage); - const cleanedKey = cleanText(rawKey); - if (!cleanedMessage) { - throw new Error("Message is empty after cleaning!"); - } - if (!cleanedKey) { - throw new Error("Key is empty after cleaning!"); - } - const encryptedMsg = encrypt(cleanedMessage, cleanedKey); - console.log("\nEncrypted Message:"); - console.log(encryptedMsg); - const decryptedMsg = decrypt(encryptedMsg, cleanedKey); - console.log("\nDecrypted Message:"); - console.log(decryptedMsg); - } catch (ve) { - console.log("Input error:", ve.message); - return; - } + const cleanedMessage = cleanText(rawMessage); + const cleanedKey = cleanText(rawKey); + + if (!cleanedMessage) throw new Error("Message is empty after cleaning!"); + if (!cleanedKey) throw new Error("Key is empty after cleaning!"); + + const encryptedMsg = encrypt(cleanedMessage, cleanedKey); + alert(`Encrypted Message: ${encryptedMsg}`); + const decryptedMsg = decrypt(encryptedMsg, cleanedKey); + alert(`Decrypted Message: ${decryptedMsg}`); } catch (e) { - console.log("An error occurred:", e.message); + alert(`An error occurred: ${e.message}`); } } main(); -