?
Solved

Fast Encryption

Posted on 2006-04-18
6
Medium Priority
?
483 Views
Last Modified: 2010-04-05
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
Comment
Question by:deathman5
  • 3
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
Jacco earned 500 total points
ID: 16481310
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
 

Author Comment

by:deathman5
ID: 16487347
yea plz give me the decryption code
0
 
LVL 46

Expert Comment

by:aikimark
ID: 16498233
Have you tried TurboPower Lockbox?
http://sourceforge.net/projects/tplockbox
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:Jacco
ID: 16504815
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
 
LVL 46

Expert Comment

by:aikimark
ID: 16506041
@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
 
LVL 10

Expert Comment

by:Jacco
ID: 16532912
Do you need any other info deathman?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month17 days, 10 hours left to enroll

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question