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


code ChIn=7, ChOut=8;
int  Neg, C, Len, I, Key;
char KeyWord(80);
[Neg:= false;                                   \skip to KEYWORD
repeat C:= ChIn(8);  if C=^- then Neg:= true;  until C>=^A & C<=^Z;
Len:= 0;                                        \read in KEYWORD
repeat KeyWord(Len):= C-^A;  Len:= Len+1;  C:= ChIn(8);  until C<^A ! C>^Z;
I:= 0;                                          \initialize cycling index
repeat  C:= ChIn(1);
        if C>=^a & C<=^z then C:= C-$20;        \capitalize
        if C>=^A & C<=^Z then                   \discard non-alphas
            [Key:= KeyWord(I);  I:= I+1;  if I>=Len then I:= 0;
            if Neg then Key:= -Key;             \decrypting?
            C:= C+Key;
            if C>^Z then C:= C-26
            else if C<^A then C:= C+26;
            ChOut(0, C);
            ];
until   C=$1A;  \EOF
ChOut(0, $1A);  \encrypted file must end with EOF otherwise the decode will hang
]