Task

Implement a   Vigenère cypher,   both encryption and decryption.

The program should handle keys and text of unequal length,
and should capitalize everything and discard non-alphabetic characters. 

(If your program handles non-alphabetic characters in another way,
make a note of it.)



Related tasks

   Caesar cipher
   Rot-13
   Substitution Cipher


import system'text;
import system'math;
import system'routines;
import extensions;
 
class VCipher
{
    string encrypt(string txt, string pw, int d)
    {
        auto output := new TextBuilder();
        int pwi := 0;
 
        string PW := pw.upperCase();
 
        txt.upperCase().forEach:(t)
        {
            if(t >= $65)
            {
                int tmp := t.toInt() - 65 + d * (PW[pwi].toInt() - 65);
                if (tmp < 0)
                {
                    tmp += 26
                };
                output.write((65 + tmp.mod:26).toChar());
                pwi += 1;
                if (pwi == PW.Length) { pwi := 0 }
            }
        };
 
        ^ output.Value
    }
}
 
public program()
{
    var v := new VCipher();
 
    var s0 := "Beware the Jabberwock, my son! The jaws that bite, the claws that catch!";
    var pw := "VIGENERECIPHER";
 
    console.printLine(s0,newLine,pw,newLine);
    var s1 := v.encrypt(s0, pw, 1);
    console.printLine("Encrypted:",s1);
    s1 := v.encrypt(s1, "VIGENERECIPHER", -1);
    console.printLine("Decrypted:",s1);
    console.printLine("Press any key to continue..");
    console.readChar()
}