How To Decrypt Strings with Vigenère Cipher Made Easy Using a Custom Function in JavaScript
Mastering Vigenère Cipher Decryption in JavaScript
4 min readJun 6, 2023
JavaScript Challenges: Beginner to Master — Task #51
Create a function that encrypts a string using the Vigenère cipher.
The function should take a plaintext string and a keyword as arguments and return the encrypted ciphertext. The keyword should be repeated as necessary to match the length of the plaintext.
The Solution to Challenge Task #50
Complete Solution of Task #51
// Solution to Challenge 50
const plaintext = "HELLO WORLD";
const keyword = "SECRET";
const ciphertext = vigenereEncrypt(plaintext, keyword);
console.log(ciphertext); // Outputs: XMCKL QBKHU
function vigenereEncrypt(plaintext, keyword) {
const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
const input = plaintext.toUpperCase();
const key = keyword.toUpperCase();
let result = "";
let keyIndex = 0;
for (let i = 0; i < input.length; i++) {
const char = input[i];
if (alphabet.includes(char)) {
const charIndex = alphabet.indexOf(char);
const keyCharIndex = alphabet.indexOf(key[keyIndex % key.length])…