Encrypt a function in Delphi

Asked

Viewed 609 times

0

Is it possible to encrypt functions in Delphi ? Any type of encryption.... example, I need to encrypt this function:

  function REspacos(const str: String): string;
const
  cDouble = ' ';
   cOne = '';
begin
   result:=Str;
   while pos(cDouble,result) > 0 do
      result:=StringReplace(result,cDouble,cOne,[rfReplaceAll]);
end;

I would like to encrypt it to be unreadable for the final client, more clear, to work normal on Delphi.

  • 3

    When you compile it is unreadable for the final client. But if you want to know if it is possible to prevent decompilation, no, it is not possible. Of course, any decompilation will not result in code as readable as the original. Well, in theory, I’ve seen code so badly written, that decompilation has improved the code :D. If it’s not this, edit the question and explain better what you want.

  • @Bigown, the question is spelled correctly. I need to ENCRYPT the function, I believe you read wrong, for example using Base64 , I know STRINGS to encrypt quiet, more and an entire function is possible to encrypt?

  • First, Base64 does not encrypt anything, it encodes. They are completely different things with different purposes and results. Second, I am reading what is written. If you’re saying you want to encrypt the function’s source code, then just do as you do with any string, a source code is a string. But I don’t know what that would do. After all, the encrypted source code couldn’t be used to compile anything. If it is none of these hypotheses, then you need to explain what you want otherwise. Repeating what has already been written does not help.

  • Will you deliver the fonts to the customer and don’t want them to see what the function does? Or will you deliver the executable and be afraid of reverse engineering?

  • @Filipe.Fonseca, that’s right, I’m going to give the source to the client, and I don’t want him to see some of the features I used. If he opens the source through Delphi, normal source code will appear, but some functions are encrypted. And if it goes to compile the project, it has to be compiled normally even with the encrypted functions, even if I create another DECRYPT function has no problem.

  • And how do you expect to call this function decrypt before compiling? The code can only be compiled if it calls another application of yours? Even if it is this, it is still possible to circumvent this. If you are going to do this, does the contract you have with the customer allow you to encrypt these functions? Otherwise, there is no point in a technical solution that will bring legal problems. But one can "protect" certain functions, then the Compile as library and delivered only the library of these functions, without the sources.

  • Got it, thanks @bigown for the tips.

Show 2 more comments

1 answer

2


Easy answer: You can’t.


Contour:

I don’t even want to get into the merits of why, so that the compiler "understand" what was written, the code must be readable.

One way around it would be to create a DLL containing the functions "hidden" and release it along with the sources.

It follows way of making withdrawal from here:

Go on File|New|Other|DLL Wizard;

Replace the source created by this:

library TestLibrary;

uses SysUtils, Classes, Dialogs;

procedure DllMessage; export;
begin
  ShowMessage('Olá Mundo') ;
end;

exports DllMessage;

begin
end. 

Create a new project (to test), include a button in the form (button1) and replace the source with the following:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes,
  Graphics, Controls, Forms, Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject) ;
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  Form1: TForm1;

 procedure DllMessage; external 'SimpleMessageDLL.dll';

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject) ;
begin
  DllMessage;
end;

end. 

By clicking the button, the system will call the function created in the DLL. Change the function by what you need and distribute the DLL to the source stating that it is needed.

  • it is also possible to put the "secret" functions in a separate Unit and send only the *.dcu file

Browser other questions tagged

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