Recent

Author Topic: Is possible? [solved]  (Read 10798 times)

ezlage

  • Guest
Is possible? [solved]
« on: December 04, 2011, 02:49:25 pm »
Friends,
I'm here one more time, thanks for all!

I'm developing an application that run in USB disks.
Isn't a malicious software, ok?

My application uses encrypted DBF with DCPCRYPT, but when I run the application is necessary decrypt it.

There are some way to decrypt in memory to make 'inserts' and 'selects'? After, I will encrypt the DBF and write in disk again.

Is possible?

Sorry by my poor English.
« Last Edit: December 07, 2011, 04:14:36 pm by ezlage »

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Is possible?
« Reply #1 on: December 04, 2011, 04:25:32 pm »
You can use streams to do this.
-Open your file in a TFileStream.
-decrypt TFileStream to a new stream
-  Tdbf.UserStream:= YourDecryptedStream;
-  Tdbf.Storage:=stoMemory;
-  Tdbf.Open;

When finished
-  Tdbf.Close
-  Encrypt YourDecryptedStream to TFileStream
-  free stream(s)


ezlage

  • Guest
Re: Is possible?
« Reply #2 on: December 06, 2011, 12:47:33 am »
Thank you, friend!

I still have trouble doing this.
I want to decrypt a file to memory, but no decrypted information can be saved in disk.

What I need to change in this code?
Code: [Select]
procedure TForm1.btnDecryptClick(Sender: TObject);
  var
    Cipher: TDCP_rc4;
    KeyStr: string;
    Source, Dest: TFileStream;
  begin
    KeyStr:= '';
    if InputQuery('Passphrase','Enter passphrase',KeyStr) then  // get the passphrase
    begin
      try
        Source:= TFileStream.Create(boxInputFile.Text,fmOpenRead);
        Dest:= TFileStream.Create(boxOutputFile.Text,fmCreate);
        Cipher:= TDCP_rc4.Create(Self);
        Cipher.InitStr(KeyStr,TDCP_sha1);              // initialize the cipher with a hash of the passphrase
        Cipher.DecryptStream(Source,Dest,Source.Size); // decrypt the contents of the file
        Cipher.Burn;
        Cipher.Free;
        Dest.Free;
        Source.Free;
        MessageDlg('File decrypted',mtInformation,[mbOK],0);
      except
        MessageDlg('File IO error',mtError,[mbOK],0);
      end;
    end;
  end;

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Is possible?
« Reply #3 on: December 06, 2011, 09:33:50 am »
Quote
What I need to change in this code?
Replace local variable Dest: TFileStream with a TForm1.YourDecryptedStream:TMemoryStream, create it with YourDecryptedStream:= TMemoryStream.Create; and don't free YourDecryptedStream at the end of the procedure but after "-  Encrypt YourDecryptedStream to TFileStream"


ezlage

  • Guest
Re: Is possible?
« Reply #4 on: December 06, 2011, 10:08:50 am »
Thank you very much!
I will test!

ezlage

  • Guest
Re: Is possible?
« Reply #5 on: December 06, 2011, 04:20:56 pm »
Friend,

See:

To first time encryption of DBF file (Working fine!):
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var
  Cipher: TDCP_rc4;
  KeyStr: string='teste';
  Source: TFileStream;
begin
  Source:=TFileStream.Create('teste1.dbf',fmOpenRead);
  ez:=TMemoryStream.Create; //ez is a variable declared in "var" section of the form1
  Cipher:= TDCP_rc4.Create(Self);
  Cipher.InitStr(KeyStr,TDCP_sha1);
  Cipher.DecryptStream(Source,ez,Source.Size);
  Cipher.Burn;
  Cipher.Free;
  Source.Free;
  Dbf1.UserStream:=ez;
  Dbf1.Storage:=stoMemory;
  Dbf1.Open;
end;

To first time decryption of DBF file in memory (Working fine!):
Code: [Select]
procedure TForm1.Button2Click(Sender: TObject);
var
  Cipher: TDCP_rc4;
  KeyStr: string='teste';
  Source, Dest: TFileStream;
begin
  Source:= TFileStream.Create('teste.dbf',fmOpenRead);
  Dest:= TFileStream.Create('teste1.dbf',fmCreate);
  Cipher:= TDCP_rc4.Create(Self);
  Cipher.InitStr(KeyStr,TDCP_sha1);
  Cipher.EncryptStream(Source,Dest,Source.Size);
  Cipher.Burn;
  Cipher.Free;
  Dest.Free;
  Source.Free;
end;

To encrypt the DBF file and save in disk (Do not crash, but write a corrupted file):
Code: [Select]
procedure TForm1.Button3Click(Sender: TObject);
var
  Cipher: TDCP_rc4;
  KeyStr: string='teste';
  Dest: TFileStream;
begin
  Dbf1.Close;
  Dest:=TFileStream.Create('teste2.dbf',fmCreate);
  Cipher:= TDCP_rc4.Create(Self);
  Cipher.InitStr(KeyStr,TDCP_sha1);
  Cipher.EncryptStream(ez,Dest,ez.Size);
  Cipher.Burn;
  Cipher.Free;
  Dest.Free;
  ez.Free;
end;

What is wrong with third procedure?

Thank you again!
« Last Edit: December 06, 2011, 05:57:17 pm by ezlage »

ezlage

  • Guest
Re: Is possible?
« Reply #6 on: December 07, 2011, 03:02:38 pm »
Anybody?

ludob

  • Hero Member
  • *****
  • Posts: 1173
Re: Is possible?
« Reply #7 on: December 07, 2011, 04:03:31 pm »
I missed your previous message  :(

Add the 2nd line to your code.
Code: [Select]
Dbf1.Close;
ez.position:=0;   //"rewind" stream before encrypting.
This is a common problem with streams. In the first 2 procedures, the streams where just opened and the "cursor" was at the beginning of the stream. When accessing it as a data source, the "cursor" can be anywhere.




ezlage

  • Guest
Re: Is possible?
« Reply #8 on: December 07, 2011, 04:14:17 pm »
Ludob, thank you very much!


Swirl

  • New Member
  • *
  • Posts: 24
Re: Is possible? [solved]
« Reply #9 on: March 08, 2020, 09:14:56 am »
Thx again ludob after 9 years.

I also missed the line...

Code: Pascal  [Select][+][-]
  1. ez.position:=0;   //"rewind" stream before encrypting.

Now my encryption ist working.
Kind Regards,
Mike
Working with Lazarus 2.0.12 - FPC 3.2.0 on Win/Mint/elementaryOS
Playing with Lazarus on macOS 10.15 / Catalina

 

TinyPortal © 2005-2018