Witam mam problem napisalem program szyfrujacy i deszyfrujacy playfair na informatyke. Nauczyciel do mnie ze program sie zaiwsza nie wiem co mu sie zawiesza :P, ale dla pewnosci zapytam się was czy wszystko jest ok
{
#################################
Algorytm szyfrujacy playfair
#################################
}
program playfair_enc_dec;
uses crt;
{-------------------------------
Zmienne
-------------------------------
}
var
tablica: array [1..5,1..5] of char;
klucz:string;
m,n:byte;
temp:string;
chose:byte;
{
-------------------------------
Zmiana liter na duze i
usuniecie spacji z textu
-------------------------------
}
function zmien(txt:string):string;
var
temp:string;
ascii:byte;
i:integer;
begin
temp:='';
for i:=1 to length(txt) do
begin
if ord(upcase(txt[i])) in [65..91] then
if (Upcase(txt[i])='J') then temp:=temp+'I' else temp:=temp+Upcase(txt[i]);
end;
zmien:=temp;
end;
{
-------------------------------
Usuniecie powtarzajacych sie liter
-------------------------------
}
function usun(txt:string):string;
var
i,j:integer;
_usun,znak:string;
begin
_usun:='';
for i:=1 to length(txt) do
begin
znak:=txt[i];
for j:=i-1 downto 1 do if (txt[i]=txt[j]) then znak:='';
_usun:=_usun+znak;
end;
usun:=_usun;
end;
{
-------------------------------
Wypelnienie tablicy
wraz z kluczem
--------------------------------
}
procedure alfabet(klucz:string);
var
i,j,k:byte;
tab: array[1..25] of char;
begin
klucz:=zmien(klucz);
klucz:=usun(klucz);
for i:=1 to length(klucz) do tab[i]:=klucz[i];
for i:=i+1 to 25 do
for j:=0 to 25 do
begin
for k:=1 to i do if char(j+65) = tab[k] then break;
if k=i then
begin
if (j+65<>74) then begin
tab[i]:=char(j+65);
break;
end;
end;
end;
k:=1;
for i:=1 to 5 do
for j:=1 to 5 do
begin
tablica[i][j]:=tab[k];
inc(k);
end;
end;
{ Koniec "alfabet" }
{
----------------------------------
Procedura szyfrujaca podany tekst
---------------------------------
}
procedure encode(tekst:string);
var
i,j,k,x1,x2,y1,y2,temp:byte;
z1,z2:char;
_encode:string;
begin
tekst:=zmien(tekst);
if (length(tekst) mod 2 <> 0) then tekst:=tekst+'X';
k:=1;
_encode:='';
while k<=length(tekst) do
begin
z1:=tekst[k];
z2:=tekst[k+1];
if z1=z2 then begin z2:='X'; k:=k-1; tekst:=tekst+'X'; end;
for i:=1 to 5 do
for j:=1 to 5 do
begin
if z1=tablica[i][j] then begin x1:=i; y1:=j; end;
if z2=tablica[i][j] then begin x2:=i; y2:=j; end;
end;
k:=k+2;
if y1=y2 then
begin
if x1=5 then x1:=1 else x1:=x1+1;
if x2=5 then x2:=1 else x2:=x2+1;
end;
if x1=x2 then
begin
if y1=5 then y1:=1 else y1:=y1+1;
if y2=5 then y2:=1 else y2:=y2+1;
end;
if (x1<>x2) and (y1<>y2) then
begin
temp:=y1;
y1:=y2;
y2:=temp;
end;
_encode:=_encode+tablica[x1,y1]+tablica[x2,y2];
end;
Writeln(_encode);
end;
{ Koniec szyfrowania }
{
------------------------------------
Procedura deszyfrujaca podany tekst
------------------------------------
}
procedure decode(tekst:string);
var
i,j,k,x1,x2,y1,y2,temp:byte;
z1,z2:char;
_decode:string;
begin
tekst:=zmien(tekst);
k:=1;
_decode:='';
while k<=length(tekst) do
begin
z1:=tekst[k];
z2:=tekst[k+1];
for i:=1 to 5 do
for j:=1 to 5 do
begin
if z1=tablica[i][j] then begin x1:=i; y1:=j; end;
if z2=tablica[i][j] then begin x2:=i; y2:=j; end;
end;
k:=k+2;
if y1=y2 then
begin
if x1=1 then x1:=5 else x1:=x1-1;
if x2=1 then x2:=5 else x2:=x2-1;
end;
if x1=x2 then
begin
if y1=1 then y1:=5 else y1:=y1-1;
if y2=1 then y2:=5 else y2:=y2-1;
end;
if (x1<>x2) and (y1<>y2) then
begin
temp:=y1;
y1:=y2;
y2:=temp;
end;
_decode:=_decode+tablica[x1,y1]+tablica[x2,y2];
end;
Writeln(_decode);
end;
{ Koniec deszyfrowania }
{
################################
Program glowny
################################
}
begin
clrscr;
Writeln('-------------------------------');
Writeln(' Algorytm szyfrujacy playfair ');
Writeln('-------------------------------');
Writeln('Wprowadz klucz:');
readln(temp);
alfabet(temp);
Writeln('Co chcesz zrobic:');
Writeln('1. Kodowanie');
Writeln('2. Odkodowanie');
Readln(chose);
Writeln('Wprowadz tekst');
Readln(temp);
case chose of
1: encode(temp);
2: decode(temp);
end;
readln;
end.