• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 487
  • Last Modified:

Fast Encryption

hello,
I need a code for a fast encryption
I mean I want to encrypt like 200 files, and I need to do it quickly
all the codes are very slow...
0
deathman5
Asked:
deathman5
  • 3
  • 2
1 Solution
 
JaccoCommented:
How good does the encryption need to be?

Generally the better the encryption the slower the routine will be.

When the encryption security can be low very fast methods can be created.

For example you could only invert the data using the following code:

type
  TEncryptionStream = class(TStream)
  private
    fStream: TStream;
  public
    constructor Create(const aTargetStream: TStream);
    function Write(const Buffer; Count: Longint): Longint; override;
  end;

{ TEncryptionStream }

constructor TEncryptionStream.Create(const aTargetStream: TStream);
begin
  fStream := aTargetStream;
end;

function TEncryptionStream.Write(const Buffer; Count: Integer): Longint;
var
  lBuffer: string;
  liChar: Integer;
begin
  Result := Count;
  SetLength(lBuffer, Result);
  Move(Buffer, PChar(@lBuffer[1])^, Result);
  for liChar := 1 to Length(lBuffer) do
    if lBuffer[liChar] < #128 then
      lBuffer[liChar] := Chr(Ord(lBuffer[liChar]) + 128)
    else
      lBuffer[liChar] := Chr(Ord(lBuffer[liChar]) - 128);
  fStream.Write(Pointer(lBuffer)^, Result);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  lSS, lTS: TFileStream;
  lES: TEncryptionStream;
  t: TDateTime;
begin
  t := Now;
  lSS := TFileStream.Create('file1', fmOpenRead);
  try
    lTS := TFileStream.Create('tst', fmCreate);
    try
      lES := TEncryptionStream.Create(lTS);
      try
        lES.CopyFrom(lSS, lSS.Size);
      finally
        lES.Free;
      end;
    finally
      lTS.Free;
    end;
  finally
    lSS.Free;
  end;
  Memo1.Lines.Add(FormatDateTime('ss:zzz', Now -t));
end;

This encodes a 365KB file in just 0.031 seconds.

Let me know if you need the code for the decryption stream as well.

Regards Jacco
0
 
deathman5Author Commented:
yea plz give me the decryption code
0
 
aikimarkCommented:
Have you tried TurboPower Lockbox?
http://sourceforge.net/projects/tplockbox
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
JaccoCommented:
Here is the decompression part.

I also use tplockbox but do not now how well it performs.

Regards Jacco

type
  TDecompressionStream = class(TStream)
  private
    fStream: TStream;
  public
    constructor Create(const aTargetStream: TStream);
    function Read(var Buffer; Count: Longint): Longint; override;
  end;

{ TDecryptionStream }

constructor TDecompressionStream.Create(const aTargetStream: TStream);
begin
  fStream := aTargetStream;
end;

function TDecompressionStream.Read(var Buffer; Count: Integer): Longint;
var
  lBuffer: string;
  liChar: Integer;
begin
  Result := fStream.Read(Buffer, Count);
  SetLength(lBuffer, Result);
  Move(Buffer, PChar(@lBuffer[1])^, Result);
  for liChar := 1 to Length(lBuffer) do
    if lBuffer[liChar] < #128 then
      lBuffer[liChar] := Chr(Ord(lBuffer[liChar]) + 128)
    else
      lBuffer[liChar] := Chr(Ord(lBuffer[liChar]) - 128);
  Move(PChar(@lBuffer[1])^, Buffer, Result);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  lSS, lTS: TFileStream;
  lDS: TDecompressionStream;
  t: TDateTime;
begin
  t := Now;
  lSS := TFileStream.Create('tst', fmOpenRead);
  try
    lDS := TDecompressionStream.Create(lSS);
    try
      lTS := TFileStream.Create('dec.exe', fmCreate);
      try
        lTS.CopyFrom(lDS, lSS.Size);
      finally
        lTS.Free;
      end;
    finally
      lDS.Free;
    end;
  finally
    lSS.Free;
  end;
  Memo1.Lines.Add(FormatDateTime('ss:zzz', Now -t));
end;
0
 
aikimarkCommented:
@deathman5

"Fast Encryption" isn't very specific.  Can you help us better understand your problem?
1. How big are these 200 files? (min, max, avg)
2. Where are the 200 files? (local hard drive, file server)
3a. What kind of files are they?
3b. Can these files be opened by another user at the time you want to encrypt them?
4. What are you willing to trade-off for speed?

More on (4)...
The encryption that JACCO has provided is very simple and the actual 'encryption' statements will only consume a fraction of the total time.  Most of the time will be spent with File I/O, open, close.  If you need to protect your files from crackers, you should consider a more robust encryption scheme.  If simply discouraging prying by end users, this might be sufficient.  Your decision depends on the value of the data in these files.
0
 
JaccoCommented:
Do you need any other info deathman?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now