Шифрование текста 2
{$I-,R-}
Unit Crypter;
interface
Uses Objects;
procedure EnCrypt(var Pntr: Array of Char; ArrLen: Word; password: string);
{ - Закpиптовать блок }
procedure DeCrypt(var Pntr: Array of Char; ArrLen: Word; password: string);
{ - Раскиптовать блок }
procedure EnCryptStream(var st: tStream; Password: String);
{ - Закpиптовать поток }
procedure DeCryptStream(var st: tStream; Password: String);
{ - Раскиптовать поток }
implementation
procedure EnCrypt(var Pntr: Array of Char; ArrLen:Word; password: string);
var
len,pcounter: byte;
x:Word;
begin
len := length(password) div 2;
pcounter := 1;
for x:=0 to ArrLen-1 do begin
Pntr[x] := chr(ord(password[pcounter]) + ord(Pntr[x]) + len);
inc(pcounter);
if pcounter > length(password) then pcounter := 1;
end;
end;
procedure DeCrypt(var Pntr: Array of Char; ArrLen:Word; password: string);
var
len,pcounter: byte;
x:Word;
begin
len := length(password) div 2;
pcounter := 1;
for x:=0 to ArrLen-1 do begin
Pntr[x] := chr(ord(Pntr[x]) - ord(password[pcounter]) - len);
inc(pcounter);
if pcounter > length(password) then pcounter := 1;
end;
end;
type
pBuffer = ^tBuffer;
tBuffer = Array[1..$FFFF] of Char;
procedure EnCryptStream(var st: tStream; Password: String);
var
buf: pBuffer;
StSize, StPos, p: Longint;
begin
if (@st=nil) or (Password='') then exit;
New(buf);
StPos:=st.GetPos;
StSize:=st.GetSize;
st.Reset;
st.Seek(0);
repeat
p:=st.GetPos;
if SizeOf(Buf^)> St.GetSize-St.GetPosthen st.Read(buf^,St.GetSize-St.GetPos)
else st.Read(buf^,SizeOf(Buf^));
EnCrypt(buf^,SizeOf(buf^),password);
st.Reset;
st.Seek(p);
st.Write(buf^,SizeOf(Buf^));
until (St.GetSize=St.GetPos);
st.Seek(StSize);
st.Truncate;
st.Seek(StPos);
Dispose(buf);
end;
procedure DeCryptStream(var st: tStream; Password: String);
var
buf: pBuffer;
StSize, StPos, p: Longint;
begin
if (@st=nil) or (Password='') then exit;
New(buf);
StPos:=st.GetPos;
StSize:=st.GetSize;
st.Reset;
st.Seek(0);
repeat
p:=st.GetPos;
if SizeOf(Buf^)> St.GetSize-St.GetPosthen st.Read(buf^,St.GetSize-St.GetPos)
else st.Read(buf^,SizeOf(Buf^));
DeCrypt(buf^,SizeOf(buf^),password);
st.Reset;
st.Seek(p);
st.Write(buf^,SizeOf(Buf^));
until (St.GetSize=St.GetPos);
st.Seek(StSize);
st.Truncate;
st.Seek(StPos);
Dispose(buf);
end;
end.
|
|