Update vigenere.js

This commit is contained in:
MANDEM_VIGNESH_KUMAR_REDDY 2025-02-18 18:30:50 +05:30 committed by GitHub
parent 27c9330b9f
commit 4baa26336d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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) { function cleanText(text) {
const textNoSpaces = text.replace(/ /g, ""); const textNoSpaces = text.replace(/ /g, "");
if (/[^A-Za-z0-9]/.test(textNoSpaces)) { if (/[^A-Za-z0-9]/.test(textNoSpaces)) {
@ -6,6 +12,12 @@ function cleanText(text) {
return textNoSpaces; 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) { function encrypt(message, key) {
message = message.toLowerCase(); message = message.toLowerCase();
key = key.toLowerCase(); key = key.toLowerCase();
@ -14,7 +26,6 @@ function encrypt(message, key) {
const encrypted = []; const encrypted = [];
let keyIndex = 0; let keyIndex = 0;
try {
for (const char of message) { for (const char of message) {
if (combinedAlphabet.includes(char)) { if (combinedAlphabet.includes(char)) {
const mIndex = combinedAlphabet.indexOf(char); const mIndex = combinedAlphabet.indexOf(char);
@ -26,11 +37,14 @@ function encrypt(message, key) {
} }
} }
return encrypted.join(""); return encrypted.join("");
} catch (e) {
throw new Error("Encryption error: " + e.message);
}
} }
/**
* 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) { function decrypt(cipherText, key) {
cipherText = cipherText.toLowerCase(); cipherText = cipherText.toLowerCase();
key = key.toLowerCase(); key = key.toLowerCase();
@ -39,7 +53,6 @@ function decrypt(cipherText, key) {
const decrypted = []; const decrypted = [];
let keyIndex = 0; let keyIndex = 0;
try {
for (const char of cipherText) { for (const char of cipherText) {
if (combinedAlphabet.includes(char)) { if (combinedAlphabet.includes(char)) {
const cIndex = combinedAlphabet.indexOf(char); const cIndex = combinedAlphabet.indexOf(char);
@ -51,38 +64,28 @@ function decrypt(cipherText, key) {
} }
} }
return decrypted.join(""); return decrypted.join("");
} catch (e) {
throw new Error("Decryption error: " + e.message);
}
} }
/**
* The main function to clean, encrypt, and decrypt messages.
*/
function main() { function main() {
try { try {
const rawMessage = prompt("Enter the message: "); const rawMessage = prompt("Enter the message: ");
const rawKey = prompt("Enter the key: "); const rawKey = prompt("Enter the key: ");
try {
const cleanedMessage = cleanText(rawMessage); const cleanedMessage = cleanText(rawMessage);
const cleanedKey = cleanText(rawKey); const cleanedKey = cleanText(rawKey);
if (!cleanedMessage) {
throw new Error("Message is empty after cleaning!"); if (!cleanedMessage) throw new Error("Message is empty after cleaning!");
} if (!cleanedKey) throw new Error("Key is empty after cleaning!");
if (!cleanedKey) {
throw new Error("Key is empty after cleaning!");
}
const encryptedMsg = encrypt(cleanedMessage, cleanedKey); const encryptedMsg = encrypt(cleanedMessage, cleanedKey);
console.log("\nEncrypted Message:"); alert(`Encrypted Message: ${encryptedMsg}`);
console.log(encryptedMsg);
const decryptedMsg = decrypt(encryptedMsg, cleanedKey); const decryptedMsg = decrypt(encryptedMsg, cleanedKey);
console.log("\nDecrypted Message:"); alert(`Decrypted Message: ${decryptedMsg}`);
console.log(decryptedMsg);
} catch (ve) {
console.log("Input error:", ve.message);
return;
}
} catch (e) { } catch (e) {
console.log("An error occurred:", e.message); alert(`An error occurred: ${e.message}`);
} }
} }
main(); main();