#1 2011-05-11 20:24:12

randydom
Member
Registered: 2011-05-11
Posts: 5

Using TAESWriteStream in SynCrypto

Hi in my Project i use the Zlib Compression for En/DeCrypting Streams on the Fly :
This how i declare It .

type
  TCompressionLevel = (clNone, clFastest, clDefault, clMax);
  TMemStream = class(TMemoryStream);

  TCompressStream = class(TStream)
  private
    FFStream: TStream;
    FMStream: TStream;
    FCompressionLevel: TCompressionLevel;
    FMode:Word;
  protected
   procedure SetNewSize(NewSize: Longint); override;
  public
    constructor Create(const AFileStream: TStream; AMode: Word; CompressionLevel: TCompressionLevel); overload;
    destructor Destroy; override;
    function Read(var Buffer; Count: Longint): Longint; override;
    function Write(const Buffer; Count: Longint): Longint; override;
    function Seek(Offset: Longint; Origin: Word): Longint; override;
  end;
//*
constructor TCompressStream.Create(const AFileStream: TStream; AMode: Word; CompressionLevel: TCompressionLevel);
var
 Dcmp: TDecompressionStream;
begin
  inherited Create;
  FMode := Mode;
  FCompressionLevel := CompressionLevel;
  FFStream := AFileStream;
  FMStream := TMemoryStream.Create;
  if Mode and fmCreate = 0 then
  begin
    FMStream.Position := 0;
    Dcmp := TDecompressionStream.Create(FFStream);
    FMStream.CopyFrom(Dcmp, Dcmp.Size);
    FMStream.Position := 0;
    Dcmp.Free;
  end;
end;

destructor TCompressStream.Destroy;
var
  Ccmp: TCompressionStream;
begin
  if (FMode and fmCreate = fmCreate) or (FMode and fmOpenWrite = fmOpenWrite) then
  begin
     FFStream.Position := 0;
     FMStream.Position := 0;
    Ccmp := TCompressionStream.Create(TCompressionLevel(FCompressionLevel),FFStream);
    Ccmp.CopyFrom(FMStream, FMStream.Size);
    Ccmp.Free;
  end;
 If Assigned(FMStream) Then FreeAndNil(FMStream);
 If Assigned(FFStream) Then FreeAndNil(FFStream);
  inherited;
end;

function TCompressStream.Seek(Offset: Longint; Origin: Word): Longint;
begin
  Result := FMStream.Seek(Offset, Origin);
end;

procedure TCompressStream.SetNewSize(NewSize: Integer);
begin
  TMemStream(FMStream).SetNewSize(NewSize);
end;

function TCompressStream.Read(var Buffer; Count: Integer): Longint;
begin
  Result :=FMStream.Read(Buffer, Count);
end;

function TCompressStream.Write(const Buffer; Count: Integer): Longint;
begin
  Result := FMStream.Write(Buffer, Count);
end;

My Question is : How could i use the SynCrypto TAESWriteStream the Same Way .
Because the User will be asked either to Compress or Encrypt his File or Both .


many thanks

Last edited by randydom (2011-05-11 20:26:34)

Offline

#2 2011-05-11 23:42:08

randydom
Member
Registered: 2011-05-11
Posts: 5

Re: Using TAESWriteStream in SynCrypto

I use this way :

function TFileHolder.CreateStorage(const ASourceFile: string;ACompressed:Boolean;AMode: Word): TStream;
begin
if (AMode and fmCreate = fmCreate) then
Begin
Case ACompressed of
True:
begin
// Create Compressed
  Result := TFileContainer.Create(ASourceFile,True, fmCreate);// Store the Stream in TFileContainer +Compression Status
  Result := TCompressStream.Create(Result, AMode,vclMax); // Compresse the Stream in TFileContainer
end;
False:
Result := TFileContainer.Create(ASourceFile,False, fmCreate);// Store the Stream in TFileContainer +Compression Status
end;
end
else
.
.
end;

Last edited by randydom (2011-05-11 23:44:53)

Offline

#3 2011-05-12 05:11:02

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,231
Website

Re: Using TAESWriteStream in SynCrypto

First of all, you could take a look at our zip units, and their associated zlib unit (faster than the one you are using):
See http://synopse.info/forum/viewtopic.php?id=48


A standard AES compressed format in zip

You'll have to change the zip headers content also.
Using zlib is not enough, it's only about compressing data, not creating a zip archive file (with file names, time, size, attributes...).

Then add the AES compression according to this doc:
See http://www.winzip.com/aes_info.htm

And ensure that the AES compression will use the same mode as in zip.
See http://synopse.info/forum/viewtopic.php?id=116

Then you could be able to send some open source code if you reach your goal.


A non standard compressed encryption format

As you stated, you can have a non standard way of storing safely data.

What is the amount of your data?
If it fits in memory (up to 200 MB e.g.), it's much more easy and faster to process in memory rather than by streams.

Take a look at the TMemoryMap object in SynCommons to map an existing file in memory.
Then use the AESSHA256() to encrypt the data AFTER compression and decrypt it BEFORE decompression.
It's a very convenient way of adding cypher to a memory block, by supplying a password. It will use the secure SHA-256 for the hashing.

Offline

#4 2011-05-12 12:31:55

randydom
Member
Registered: 2011-05-11
Posts: 5

Re: Using TAESWriteStream in SynCrypto

Thank you Sir ab .
My Project is a Virtual File System , so the User will / can Use it with very huge files ** from 1 MB to 5 GB ** , so imagine a user is adding a file with 2 GB size and more imagine  he wants to Compress / Encrypt it ; and as you know all these operations ( Compression+Encryption ) will be done on the Fly ...!!!!!

I did tests with the Current used Compression method and  results were  impressive mainly with Speed . that's why i was asked by ONE of my Clients to add the Encryption Feature ;
Encryption means each file added will be encrypted On the Fly .

Offline

Board footer

Powered by FluxBB