Generate .exe from my Program

2

Well I'm in doubt I saw in a forum something about this Content found in the forum and I am looking for more explanations and examples

I am developing a Database Auto-Backup generating your .SQL file and compressing into a .zip or .rar file, my doubt is the following I wanted to create an executable in which it will encapsulate the .zip file or. rar into it, examples of encapsulating files in an executable found here , I also know how to do this encapsulation via code in Delphi by 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 open 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 kind of customizations of type put password in the executable thus protecting the file.     

asked by anonymous 15.05.2018 / 16:37

1 answer

1

As we talked in the comments, I'm going to use the DCPCrypt library 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 #

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

This example was created using dcpcrypt official demos and documentation.

    
16.05.2018 / 16:39