Anda di halaman 1dari 4

type

mat = array[1..2,1..100] of integer;


procedure convert(inp:string; var asci:mat;disp:boolean);
var
i,j,k,l: integer;
sp : string[1];
begin
sp := ' ';
if not length(inp) mod 2 = 0 then insert(sp,inp,length(inp)+1);
k := length(inp) div 2;
l := 0;
if disp=true then writeln('Converted message to numbers: ');
for i:=1 to 2 do
begin
for j:=1 to k do
begin
inc(l);
if inp[l] = ' ' then asci[i,j]:=0
else asci[i,j]:=ord(inp[l])-ord('a')+1;
if disp = true then write(asci[i,j], ' ');
end;
if disp = true then writeln();
end;
end;
procedure encrypt(asci,key:mat;k:integer;disp:boolean);
var
i,j:integer;
code:mat;
msg,sp,num:string;
begin
sp:= ' ';
msg:= '';
if disp=true then writeln('Encrypted matrix:');
for i:= 1 to 2 do
begin
for j:=1 to k do
begin
code[i,j]:= key[i,1]*asci[1,j] + key[i,2] * asci[2,j];
if disp = true then write(code[i,j],' ');
if code[i,j]=0 then insert(sp,msg,length(msg)+1)
else
begin
str(code[i,j],num);
insert(num,msg,length(msg)+1);
insert(sp,msg,length(msg)+1);
end;
end;
if disp = true then writeln();
end;
writeln('Encrypted message: ');
writeln(msg);
end;
procedure decrypt(asci,key:mat;k:integer;disp:boolean);
var
i,j,det,temp:integer;
msg,alp,sp:string;
code:mat;
begin
det:=(key[1,1]*key[2,2]) - (key[1,2]*key[2,1]);
if disp=true then writeln('Determinant: ',det);

temp:= key[1,1];
key[1,1]:=key[2,2];
key[2,2]:=temp;
key[1,2]:=-1*key[1,2];
key[2,1]:=-1*key[2,1];
if disp = true then writeln('Inverse of key: ');
for i:=1 to 2 do
begin
for j:=1 to 2 do
begin
key[i,j]:=det*key[i,j];
if disp = true then write(key[i,j],' ');
end;
if disp = true then writeln();
end;
msg:='';
sp:=' ';
if disp = true then writeln('Decrypted matrix: ');
for i:= 1 to 2 do
begin
for j:=1 to k do
begin
code[i,j]:= key[i,1]*asci[1,j] + key[i,2] * asci[2,j];
if disp = true then write(code[i,j],' ');
alp:=chr(code[i,j]+ord('a')-1);
if code[i,j]=0 then insert(sp,msg,length(msg)+1)
else insert(alp,msg,length(msg)+1);
end;
if disp = true then writeln();
end;
writeln('Decrypted message: ');
writeln(msg);
end;
label 1;
var
inp:string;
ans:char;
enc,disp,again:boolean;
i,j,k,l:integer;
asci,key,inpd:mat;
begin
key[1,1] := 2;
key[1,2] := -5;
key[2,1] := -1;
key[2,2] := 3;
again:=true;
while again=true do
begin
write('Do you want to encrypt or decrypt?(e/d) ');
readln(ans);
l:=0;
if ans = 'e' then enc:=true
else if ans = 'd' then enc:= false
else begin
writeln('ERROR. Command not found.');
goto 1;
end;
write('Do you want to display process?(y/n) ');
readln(ans);
if ans = 'y' then disp:=true

else if ans = 'n' then disp:=false


else begin
writeln('ERROR. Command not found.');
goto 1;
end;
if enc = true then
begin
writeln('Enter message: ');
readln(inp);
if length(inp) mod 2 = 0 then k:=length(inp) div 2
else k:=length(inp) div 2 +1;
convert(inp, asci,disp);
if disp = true then
begin
writeln('Key:');
for i:=1 to 2 do
begin
for j:=1 to 2 do
begin
write(key[i,j],' ');
end;
writeln();
end;
end;
encrypt(asci,key,k,disp);
end
else
begin
write('How many characters the message has? ');
readln(l);
writeln('Enter message: ');
for i:=1 to l do read(inpd[1,i]);
readln();
k:=l div 2;
l:=0;
if disp = true then writeln('Converted message into matr
ix:');
for i:=1 to 2 do
begin
for j:=1 to k do
begin
inc(l);
asci[i,j]:=inpd[1,l];
if disp = true then write(asci[i,j],' ');
end;
if disp = true then writeln();
end;
if disp = true then
begin
writeln('Key:');
for i:=1 to 2 do
begin
for j:=1 to 2 do
begin
write(key[i,j],' ');
end;
writeln();
end;
end;
decrypt(asci,key,k,disp);

end;
1:
writeln('Try again?(y/n) ');
readln(ans);
if ans='y' then again:=true
else if ans='n' then again:=false
else
begin
writeln('ERROR. Command not found.');
goto 1;
end;
end;
end.

Anda mungkin juga menyukai