this slowpoke moves

LZCipher String & File Crypter

Unit LZZLib.pas

unit LZZLib;

interface

uses
Classes, SysUtils, ZLib;

procedure Compress(FileIn : string; FileOut : string);
procedure Decompress(FileIn : string; FileOut : string);
function LoadDecompressToStringList(FileIn : string) : string;
procedure SaveCompressStringListToFile(StringList: string; FileOut: string);

implementation

procedure Compress(FileIn : string; FileOut : string);
var
FileI, FileO: TFileStream;
Zip: TCompressionStream;
begin
 FileI := TFileStream.Create(FileIn, fmOpenRead and fmShareExclusive);
 FileO := TFileStream.Create(FileOut, fmCreate or fmShareExclusive);
 Zip := TCompressionStream.Create(clMax, FileO);
 Zip.CopyFrom(FileI, FileI.Size);
 Zip.Free;
 FileO.Free;
 FileI.Free;
end;

procedure Decompress(FileIn : string; FileOut : string);
var
FileI, FileO: TFileStream;
DeZip: TDecompressionStream;
i: Integer;
Buf: array[0..1023]of Byte;
begin
 FileI := TFileStream.Create(FileIn, fmOpenRead and fmShareExclusive);
 FileO := TFileStream.Create(FileOut, fmCreate or fmShareExclusive);
 DeZip:=TDecompressionStream.Create(FileI);

 repeat
 i:=DeZip.Read(Buf, SizeOf(Buf));
 if i <> 0 then FileO.Write(Buf, i);
 until i <= 0;
 DeZip.Free;
 FileO.Free;
 FileI.Free;

end;

function LoadDecompressToStringList(FileIn: string): string;
var
Stream: TMemoryStream;
FileIni: TFileStream;
DeZip: TDecompressionStream;
i: Integer;
Buf: array[0..1023]of Byte;
Strings: TStringList;
begin
 Stream := TMemoryStream.Create();
 FileIni:=TFileStream.Create(FileIn, fmOpenRead and fmShareExclusive);
 DeZip:=TDecompressionStream.Create(FileIni);

 repeat
 i:=DeZip.Read(Buf, SizeOf(Buf));
 if i <> 0 then Stream.Write(Buf, i);
 until i <= 0;
 DeZip.Free;
 Stream.Seek(0, soFromBeginning);
 Strings := TStringList.Create;
 Strings.LoadFromStream(Stream);
 Stream.Free;
 FileIni.Free;
 Result := Strings.Text;
 Strings.Free;
end;

procedure SaveCompressStringListToFile(StringList: string; FileOut: string);
var
FileO: TFileStream;
Zip: TCompressionStream;
Stream: TMemoryStream;
S: string;
begin
 S := StringList;
 Stream := TMemoryStream.Create();
 Stream.WriteBuffer(Pointer(S)^, Length(S));
 Stream.Seek(0, soFromBeginning);
 FileO := TFileStream.Create(FileOut, fmCreate or fmShareExclusive);
 Zip := TCompressionStream.Create(clMax, FileO);
 Zip.CopyFrom(Stream, Stream.Size);
 Zip.Free;
 Stream.Free();
 FileO.Free;
end;

end.
Unit LZCipher.pas
unit LZCipher;

interface

uses
LZZLib;

function EnDecryptString(str : string; key: Integer) : string;
function ZLoadDecompressToStringList(FileIn: string; key: integer): string;
procedure ZSaveCompressStringListToFile(StringList: string; FileOut: string;
key: integer);

implementation

function EnDecryptString(str : string; key: integer) : string;
var
I: Integer;
OutValue : String;
begin
 for i := 1 to length (str) do
 begin
 OutValue := OutValue + char( not( ord(str[I])-key ) );
 end;
 Result := OutValue;
end;

function ZLoadDecompressToStringList(FileIn: string; key: integer): string;
var
tmp:string;
begin
 tmp := LoadDecompressToStringList(FileIn);
 tmp := EnDecryptString(tmp, key);
 result := copy(tmp,1,length(tmp)-2);
end;

procedure ZSaveCompressStringListToFile(StringList: string; FileOut: string;
key: integer);
var
tmp: string;
begin
 tmp := EnDecryptString(StringList, key);
 SaveCompressStringListToFile(tmp, FileOut);
end;

end.
Beispiele :
uses LZCipher, LZZLib

// Crypt
procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo2.Text := EnDecryptString(Memo1.Text, StrToInt(Edit1.Text));
end;

// Decrypt
procedure TForm1.Button2Click(Sender: TObject);
begin
  Memo1.Text := EnDecryptString(Memo2.Text, StrToInt(Edit1.Text));
end;

Keine Kommentare:

Kommentar veröffentlichen

Beliebte Posts

Translate