unit EncodStr;
interfaceuses
Classes;
type
TEncodedStream = class (TFileStream)
private
FKey: Char;
publicconstructor Create(const FileName: string; Mode: Word);
functionRead(var Buffer; Count: Longint): Longint; override;
functionWrite(const Buffer; Count: Longint): Longint; override;
property Key: Char read FKey write FKey default 'A';
end;
implementationconstructor TEncodedStream.Create(
const FileName: string; Mode: Word);
begininherited Create (FileName, Mode);
FKey := 'A';
end;
function TEncodedStream.Write(const Buffer;
Count: Longint): Longint;
var
pBuf, pEnc: PChar;
I, EncVal: Integer;
begin// allocate memory for the encoded buffer
GetMem (pEnc, Count);
try// use the buffer as an array of characters
pBuf := PChar (@Buffer);
// for every character of the bufferfor I := 0 to Count - 1 dobegin// encode the value and store it
EncVal := ( Ord (pBuf[I]) + Ord(Key) ) mod 256;
pEnc [I] := Chr (EncVal);
end;
// write the encoded buffer to the file
Result := inheritedWrite (pEnc^, Count);
finally
FreeMem (pEnc, Count);
end;
end;
function TEncodedStream.Read(var Buffer; Count: Longint): Longint;
var
pBuf, pEnc: PChar;
I, CountRead, EncVal: Integer;
begin// allocate memory for the encoded buffer
GetMem (pEnc, Count);
try// read the encoded buffer from the file
CountRead := inheritedRead (pEnc^, Count);
// use the output buffer as a string
pBuf := PChar (@Buffer);
// for every character actually readfor I := 0 to CountRead - 1 dobegin// decode the value and store it
EncVal := ( Ord (pEnc[I]) - Ord(Key) ) mod 256;
pBuf [I] := Chr (EncVal);
end;
finally
FreeMem (pEnc, Count);
end;
// return the number of characters read
Result := CountRead;
end;
end.
Статья Кодирование файлов раздела Синтаксис Шифрование может быть полезна для разработчиков на delphi & pascal.
Комментарии и вопросы
Материалы статей собраны из открытых источников, владелец сайта не претендует на авторство. Там где авторство установить не удалось, материал подаётся без имени автора. В случае если Вы считаете, что Ваши права нарушены, пожалуйста, свяжитесь с владельцем сайта.