Jump to content
Sign in to follow this  
direktor05

Binary file encryption/decription problem?

Recommended Posts

Hello, does anybody know of any good Delphi AES (or similar) unit to encrypt/decrypt binary files? I found plenty however none works well - there is loss of information or can only encrypt/decrypt strings. Must be able to encrypt/decrypt binary file without loss of information - after decrypting back to original the exe file must be exactly the same in size.

 

Help is needed.

Share this post


Link to post

There's plenty of them, check the link in my signature. As for your trouble, likely you do something wrong. There's no big difference between strings and binary.

Share this post


Link to post
1 hour ago, direktor05 said:

Hello, does anybody know of any good Delphi AES (or similar) unit to encrypt/decrypt binary files? I found plenty however none works well - there is loss of information or can only encrypt/decrypt strings. Must be able to encrypt/decrypt binary file without loss of information - after decrypting back to original the exe file must be exactly the same in size.

 

Help is needed.

I use Lockbox 3 for such purposes.

Share this post


Link to post

Yes, but its component. Is there any standalone unit? Like this one: https://github.com/stijnsanders/tools - has standalone units in crypto folder, but unfortunately they do not produce accurate results: https://github.com/stijnsanders/tools/tree/master/crypto 

https://github.com/stijnsanders/tools/blob/master/crypto/aes.pas - when encrypting binary file I get only 16 B file back. But no error when building/compiling.

Share this post


Link to post

This doesnt work:

 

var
  Source, Dest: TFileStream;
  SrcFile, DestFile: string;
  Start, Stop: cardinal;
  Size: integer;
  SourceKey,NewKey: AESBlock;
  Key: AES128Key;
  SrcBuf, DstBuf: array [0..16383] of byte;
  SrcSize, DstSize: integer;
  MyInBlock, MyOutBlock: AESBlock;
begin
    Source := TFileStream.Create(txtFile.Text, fmOpenRead);
    DestFile := ExtractFilePath(Application.ExeName) + 'aestemp.enc';
    Dest := TFileStream.Create(DestFile, fmCreate);
    try
      Size:=Source.Read(SourceKey,SizeOf(SourceKey));
      if Size<>SizeOf(SourceKey) then
        raise Exception.Create('Unable to read SourceKey from file.');

      FillChar( Key, SizeOf(Key), 0 );
      Move( PAnsiChar(AnsiString(txtPassword.Text))^, Key, Min( SizeOf( Key ), Length( txtPassword.Text ) ));

      //FillChar( MyInBlock, SizeOf(MyInBlock), 0 );
      //Move(Source.ReadBuffer(SrcBuf,Size), MyInBlock, Min( SizeOf( MyInBLock ), Size ));

      NewKey:=AES128Cipher(SourceKey,Key);
      Dest.WriteBuffer(NewKey,SizeOf(NewKey));
      //AES128Cipher(Source,Key)

      //Dest.WriteBuffer(AES128Cipher(Source,Key),Size);
    finally
      Dest.Free;
      Source.Free;
    end;
    showmessage('Finished');

 

Share this post


Link to post
3 hours ago, direktor05 said:

Yes, but its component. Is there any standalone unit? Like this one: https://github.com/stijnsanders/tools - has standalone units in crypto folder, but unfortunately they do not produce accurate results: https://github.com/stijnsanders/tools/tree/master/crypto 

https://github.com/stijnsanders/tools/blob/master/crypto/aes.pas - when encrypting binary file I get only 16 B file back. But no error when building/compiling.

You don't need to install the component, it is just a convenience wrapper. I'll try to create a small demo over the weekend, no time at the moment.

Share this post


Link to post

Ok thanks I think the problem with the above code is that it gives whole file buffer as input while only 16 B is acceptable. Thats why it takes first 16 B and the rest throws away. The right way should be ReadBlock 16 by 16 B until EOF and perform readblock 16B - encrypt 16B - write 16 B until EOF.

Share this post


Link to post

Should not use FileStream or MemoryStream, no stream at all, just readblock - encrypt block - write block

Share this post


Link to post
On 6/17/2022 at 7:27 PM, direktor05 said:

Ok thanks I think the problem with the above code is that it gives whole file buffer as input while only 16 B is acceptable. Thats why it takes first 16 B and the rest throws away. The right way should be ReadBlock 16 by 16 B until EOF and perform readblock 16B - encrypt 16B - write 16 B until EOF.

Attached find a little example program that definitely works :classic_cool:.

 

FileEncryption.zip

Share this post


Link to post

Ah you used LockBox component... what a shame! Real professionals don't use other people's components they make their own. But it's OK, it works.

  • Haha 2

Share this post


Link to post
8 minutes ago, direktor05 said:

Real professionals don't use other people's components they make their own.

:classic_rolleyes: Really? :classic_ohmy: And what about such a TMS?

I acknowledge that the examples should only have standard components.

Edited by Stano

Share this post


Link to post
3 hours ago, direktor05 said:

Ah you used LockBox component... what a shame! Real professionals don't use other people's components they make their own. But it's OK, it works.

Real professionals have to earn money with their works, they don't have the time to reinvent the wheel, debug and validate it. If you want to shoot yourself in the foot be my guest, but don't expect me to supply the bullets...

  • Thanks 1
  • Haha 3

Share this post


Link to post
4 hours ago, direktor05 said:

Ok boss, got it. When I solve the problem of shooting myself Ill post it here. 🙂

Quote

Brenda
What about those bank robbers? The ones that made that suicide pact. 
Damon
Did they commit suicide? 
Brenda
Obviously not Damon. 
Damon
Then they're unreliable. 
© Running man

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×