46 lines
1.1 KiB
Text
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()
|
|
}
|