Solución

@totekev_903d1abe·21/7/2026TypeScript
solution.tsTypeScript
export function vigenereEncrypt(text: string, key: string): string {

  const alphabet = [
    'A', 'B', 'C', 'D', 'E', 'F', 'G',
    'H', 'I', 'J', 'K', 'L', 'M', 'N',
    'O', 'P', 'Q', 'R', 'S', 'T', 'U',
    'V', 'W', 'X', 'Y', 'Z'
  ];

  const regex = /^[a-zA-Z]+$/;
  let result = '';
  let txtConcat = '';
  let x = 0
  let y = 0

  do {
    if (regex.test(text[y])) {

      if (!key[x]) {
        x = 0;
        txtConcat += key[x];
      } else {
        txtConcat += key[x];
      }
      x++;
    } else {
      txtConcat += text[y];
    }

    y++;
  } while (txtConcat.length < text.length);

   for (let i = 0; i < text.length; i++) {

    const textChar = text[i];

    if (regex.test(textChar)) {
      const txtConcatChar = txtConcat[i];
      const keyIndex = alphabet.indexOf(txtConcatChar.toUpperCase());
      const textIndex = alphabet.indexOf(textChar.toUpperCase());
      const modRes = mod26(keyIndex + textIndex);
      const modRes_abc = alphabet[modRes];

      if (textChar === textChar.toUpperCase()) {
        result += modRes_abc.toUpperCase();
      }

      if (textChar === textChar.toLowerCase()) {
        result += modRes_abc.toLowerCase();
      }

    } else {
      result += textChar;
    }

  }
  return result;
}


function mod26(n): number {
  return ((n % 26) + 26) % 26;
}
0respuestas
Respuestas

Aún no hay respuestas

¡Sé el primero en responder!

Escribir un comentario

Recuerda ser amable. Estás comentando la solución de otra persona. Comparte tu perspectiva de forma constructiva y respetuosa.

Debes iniciar sesión para publicar un comentario.