RosettaCodeData/Task/Caesar-cipher/Zonnon/caesar-cipher.zonnon

58 lines
1.3 KiB
Plaintext

module Caesar;
const
size = 25;
type
Operation = (code,decode);
procedure C_D(s:string;k:integer;op: Operation): string;
var
i,key: integer;
resp: string;
n,c: char;
begin
resp := "";
if op = Operation.decode then key := k else key := (26 - k) end;
for i := 0 to len(s) - 1 do
c := cap(s[i]);
if (c >= 'A') & (c <= 'Z') then
resp := resp +
string(char(integer('A') + ((integer(c) - integer('A') + key )) mod 26));
else
resp := resp + string(c)
end;
end;
return resp
end C_D;
procedure {public} Cipher(s:string;k:integer):string;
var
i: integer;
resp: string;
n,c: char;
begin
return C_D(s,k,Operation.code)
end Cipher;
procedure {public} Decipher(s:string;k:integer):string;
var
i: integer;
resp: string;
n,c: char;
begin
return C_D(s,k,Operation.decode)
end Decipher;
var
txt,cipher,decipher: string;
begin
txt := "HI";cipher := Caesar.Cipher(txt,2);decipher := Caesar.Decipher(cipher,2);
writeln(txt," -c-> ",cipher," -d-> ",decipher);
txt := "ZA";cipher := Caesar.Cipher(txt,2);decipher := Caesar.Decipher(cipher,2);
writeln(txt," -c-> ",cipher," -d-> ",decipher);
txt := "The five boxing wizards jump quickly";
cipher := Caesar.Cipher(txt,2);decipher := Caesar.Decipher(cipher,2);
writeln(txt," -c-> ",cipher," -d-> ",decipher)
end Caesar.