RosettaCodeData/Task/Vigen-re-cipher/Elena/vigen-re-cipher.elena
2024-03-06 22:25:12 -08:00

46 lines
1.1 KiB
Text

import system'text;
import system'culture;
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.toUpper();
var TXT := txt.toUpper();
foreach(char t; in TXT)
{
if (t < $65) $continue;
int tmp := t - 65 + d * (pw[pwi] - 65);
if (tmp < 0) tmp += 26;
output.write((65 + tmp.mod(26)).toChar());
pwi++;
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,newLineConstant,pw,newLineConstant);
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()
}