Generate . exe from my Program

Asked

Viewed 607 times

2

Well I’m in doubt I saw in a forum something about that Content found in the forum and I’m looking for more explanations and examples

I am developing an Auto-Backup Database generating your . SQL file and compressing into a . zip or . rar, my doubt is the following wanted to create an executable in which it will encapsulate the file . zip or . rar into it, examples of encapsulation of files in a found executable here , I also know how to do this encapsulation via code in Delphi building the code and passing the location of the file and also know that Delphi allows to compile manually.

Example of what I want and type the Winrar that generates a.rar file of its extension that is opened in your software and with possible password protection.

Explanations: I want to make an executable from mine to encapsulate a file so that the file can only be opened by this generated executable thus creating some customizations of type put password in the executable thus protecting the file.

  • What you need is to put the file extension in the windows and encrypt the contents of the file so that another program or other personnel cannot identify.

  • Question of putting the extension in the windows registry I was already studying however how to encrypt the contents of the file without corrupting it and still making only my software open it

  • To be clearer would create a Winrar of life where it is an Executable in which generates another executable with files within that

  • Your description is a little confusing. It seems to me that you want your program to encrypt the backup files, and only it can decompress them. Is this it? If yes, there is a wide range of encryption libraries that you can use for this development, e.g.: Dcpcrypt

  • That so only my program could open the file

1 answer

1


As we talked in the comments, I am passing how I use the library Dcpcrypt for encrypt/decrypt files. This can be used as a component in Delphi, it is free and open source.

Encrypt/decrypt file:

uses
  DCPcrypt2, System.Math, DCPsha512, DCPdes;

procedure EncriptaArquivo(aArquivo: string; aArquivoEncriptado: string; aHash: TDCP_hash; aCipher: TDCP_cipher; aSenha: String);
var
  CipherIV: array of byte;
  HashDigest: array of byte;
  Salt: array[0..7] of byte;
  strmInput, strmOutput: TFileStream;
  i: integer;
begin
  strmInput := nil;
  strmOutput := nil;
  try
    strmInput := TFileStream.Create(aArquivo,fmOpenRead);
    strmOutput := TFileStream.Create(aArquivoEncriptado,fmCreate);

    SetLength(HashDigest,aHash.HashSize div 8);
    for i := 0 to 7 do
      Salt[i] := Random(256);
    strmOutput.WriteBuffer(Salt,Sizeof(Salt));
    aHash.Init;
    aHash.Update(Salt[0],Sizeof(Salt));
    aHash.UpdateStr(aSenha);
    aHash.Final(HashDigest[0]);

    if (aCipher is TDCP_blockcipher) then
    begin
      SetLength(CipherIV,TDCP_blockcipher(aCipher).BlockSize div 8);
      for i := 0 to (Length(CipherIV) - 1) do
        CipherIV[i] := Random(256);
      strmOutput.WriteBuffer(CipherIV[0],Length(CipherIV));
      aCipher.Init(HashDigest[0],System.Math.Min(aCipher.MaxKeySize,aHash.HashSize),CipherIV);
      TDCP_blockcipher(aCipher).CipherMode := cmCBC;
    end
    else
      aCipher.Init(HashDigest[0],Min(aCipher.MaxKeySize,aHash.HashSize),nil);

    aCipher.EncryptStream(strmInput,strmOutput,strmInput.Size);
    aCipher.Burn;
    strmInput.Free;
    strmOutput.Free;
  except
    strmInput.Free;
    strmOutput.Free;
    MessageDlg('Um erro aconteceu no processo de encriptação',mtError,[mbOK],0);
  end;
end;


procedure DesencriptaArquivo(aArquivoEncriptado: string; aArquivoDesencriptado: string; aHash: TDCP_hash; aCipher: TDCP_cipher; aSenha: String);
var
  CipherIV: array of byte;
  HashDigest: array of byte;
  Salt: array[0..7] of byte;
  strmInput, strmOutput: TFileStream;
begin
  strmInput := nil;
  strmOutput := nil;
  try
    strmInput := TFileStream.Create(aArquivoEncriptado,fmOpenRead);
    strmOutput := TFileStream.Create(aArquivoDesencriptado,fmCreate);

    SetLength(HashDigest,aHash.HashSize div 8);
    strmInput.ReadBuffer(Salt[0],Sizeof(Salt));
    aHash.Init;
    aHash.Update(Salt[0],Sizeof(Salt));
    aHash.UpdateStr(aSenha);
    aHash.Final(HashDigest[0]);

    if (aCipher is TDCP_blockcipher) then
    begin
      SetLength(CipherIV,TDCP_blockcipher(aCipher).BlockSize div 8);
      strmInput.ReadBuffer(CipherIV[0],Length(CipherIV));
      aCipher.Init(HashDigest[0],Min(aCipher.MaxKeySize,aHash.HashSize),CipherIV);
      TDCP_blockcipher(aCipher).CipherMode := cmCBC;
    end
    else
      aCipher.Init(HashDigest[0],Min(aCipher.MaxKeySize,aHash.HashSize),nil);

    aCipher.DecryptStream(strmInput,strmOutput,strmInput.Size - strmInput.Position);
    aCipher.Burn;
    strmInput.Free;
    strmOutput.Free;
  except
    strmInput.Free;
    strmOutput.Free;
    MessageDlg('Um erro aconteceu no processo de desencriptação',mtError,[mbOK],0);
  end;
end;

Encryption example:

var
  vHash: TDCP_hash;
  vCipher: TDCP_3des;
  vSenha: String;
begin
  vSenha := 'abobrinha123';
  vHash := TDCP_sha512.Create(nil);
  vCipher := TDCP_3des.Create(nil);
  try
    EncriptaArquivo('d:\ArquivoOriginal.txt', 'd:\ArquivoEncriptado.txt', vHash, vCipher, vSenha);
  finally
    FreeAndNil(vHash);
    FreeAndNil(vCipher);
  end;
end;

Decryption example:

var
  vHash: TDCP_hash;
  vCipher: TDCP_3des;
  vSenha: String;
begin
  vSenha := 'abobrinha123';
  vHash := TDCP_sha512.Create(nil);
  vCipher := TDCP_3des.Create(nil);
  try
    DesencriptaArquivo('d:\ArquivoEncriptado.txt', 'd:\ArquivoDesencriptado.txt', vHash, vCipher, vSenha);
  finally
    FreeAndNil(vHash);
    FreeAndNil(vCipher);
  end;

end;

In the example I used the hash algorithm sha-512 and cipher 3DES, but this library has a wide range of algorithmic options, you can utilize whatever you prefer.

In the example I used a txt file to facilitate the example, but this code works for any type of file.

This example was created using the official dcpcrypt demos and documentation.

  • Thank you so much, it will help me a lot, this your most example explanation will be a hand on the wheel Thanks a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.