home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CP/M
/
CPM_CDROM.iso
/
cpm
/
draco
/
draco-1.ark
/
CRYPT.DRC
< prev
next >
Wrap
Text File
|
1986-11-12
|
3KB
|
154 lines
\util.g
channel input binary Chin;
channel output binary Chout;
file() Fin, Fout;
[256] byte Key;
[256] char CharKey @ Key;
[256] byte StartKey;
[256] ushort Table1, Table2;
bool EnCrypt;
proc nonrec process()void:
[256] byte buffer1, buffer2;
[128] byte shortBuff;
ushort i;
bool done;
done := false;
while not done do
if read(Chin; shortBuff) then
for i from 0 upto 128 - 1 do
buffer1[i] := shortBuff[i];
od;
if read(Chin; shortBuff) then
for i from 0 upto 128 - 1 do
buffer1[i + 128] := shortBuff[i];
od;
else
done := true;
fi;
for i from 0 upto 256 - 1 do
if EnCrypt then
buffer2[i] := buffer1[Table1[i]] >< Key[i];
else
buffer2[i] := buffer1[Table2[i]] >< Key[Table2[i]];
fi;
od;
if not write(Chout; buffer2) then
write(" bad write to output file.");
exit(1);
fi;
for i from 0 upto 256 - 1 do
Key[i] := Key[i] * 19 + 37;
od;
else
done := true;
fi;
od;
corp;
proc nonrec fixKey()void:
ushort i, j, s;
byte b;
s := 256 - 1;
while s ~= 0 and CharKey[s] = ' ' do
s := s - 1;
od;
i := s + 1;
j := 0;
while
CharKey[i] := CharKey[j];
if j = s then
j := 0;
else
j := j + 1;
fi;
i ~= 256 - 1
do
i := i + 1;
od;
b := 0;
for i from 1 upto s do
b := b + Key[i];
od;
for i from 0 upto 256 - 1 do
Key[i] := Key[i] >< b;
b := b * 13 + 59;
od;
StartKey := Key;
for i from 0 upto 256 - 1 do
Table1[i] := b;
Table2[b] := i;
b := b * 17 + 43;
od;
corp;
proc nonrec main()void:
*char par;
FILENAME fnin, fnout;
[15] char namein, nameout;
par := GetPar();
if par ~= nil and par* = '-' then
par := par + 1;
fi;
if par = nil or par* ~= 'D' and par* ~= 'E' or (par + 1)* ~= '\e' then
writeln("Use is: crypt -{d|e} f1.typ ... fn.typ");
else
EnCrypt := par* = 'E';
par := GetPar();
if par = nil then
writeln("Use is: crypt -{d|e} f1.typ ... fn.typ");
else
write("Key> ");
if readln(CharKey) then
fixKey();
while
write(par, ':');
SetFileName(fnin, par);
SetFileName(fnout, par);
if EnCrypt then
fnout.fn_type[0] := 'C';
fnout.fn_type[1] := 'R';
fnout.fn_type[2] := 'P';
else
fnin.fn_type[0] := 'C';
fnin.fn_type[1] := 'R';
fnin.fn_type[2] := 'P';
fi;
GetFileName(fnin, &namein[0]);
GetFileName(fnout, &nameout[0]);
if open(Chin, Fin, &namein[0]) then
if FileDestroy(fnout) then fi;
if FileCreate(fnout) then
if open(Chout, Fout, &nameout[0]) then
process();
Key := StartKey;
if not close(Chout) then
write(" error closing output file");
fi;
else
write(" can't open output file");
fi;
else
write(" can't create output file");
fi;
else
write(" can't open input file");
fi;
writeln();
par := GetPar();
par ~= nil
do
od;
fi;
fi;
fi;
corp;