topic "How it works";
[ $$0,0#00000000000000000000000000000000:Default]
[{_}%EN-US 
[s0;%- [*R6 How it works]&]
[s0;/%- &]
[s0; The software protection is composed by 2 packages :&]
[s0; &]
[s0;i150;O0; [* Protect ]package, which must be included and linked 
with user application&]
[s0; &]
[s0;i150;O0; [* ProtectEncrypt ]package, a command`-line standalone 
app which is used by post`-link custom build step to encrypt 
the executable after link process&]
[s0; &]
[s0; The usage is quite simple and transparent to build process; 
to use it is enough to :&]
[s0; &]
[s0;i150;O0; Build the [* ProtectEncrypt ]package and put its executable 
in some easy to locate place (system32 windows directory or /usr/bin 
for linux systems, for example)&]
[s0; &]
[s0;i150;O0; In main package configuration of application to protect, 
add a PROTECT flag; you can, for example, make 2 main configurations, 
one for testing, without protection, and another with protection 
for deploying purposes :&]
[s0; &]
[s0; 
@@rawimage:3850&1254
([encoded data, 32388 characters])
&]
[s0; &]
[s0;i150;O0; Add a post`-link custom build step, which run the [* ProtectEncrypt 
]code encryptor after the link phase :&]
[s0; &]
[s0; 
@@rawimage:3884&971
([encoded data, 46464 characters])
&]
[s0; &]
[s0; You can see the [* ProtectEncrypt ]invocation (here with full 
path, if you placed it on a system folder you can just use the 
app name); first parameter [* `$(OUTPATH)] represents the executable 
to be encrypted, and must not be changed; second parameter (here 
[* AABBCC...8899]) is the encryption key, which [* MUST ]be [* 128 
or 256 bits long] ([* 16 or 32 bytes]), written as an hex`-ascii 
string, every 2 chars represent 1 hex byte.&]
[s0; &]
[s0;i150;O0; Add Protect package to your app, and include its header 
:&]
[s0; &]
[s0;l160; [/ #include <Protect/Protect.h>]&]
[s0; &]
[s0;i150;O0; In your application, enclose parts of code you want 
to be encrypted by mean of PROTECT`_START`_FUNC(decrFunc) and 
PROTECT`_END`_FUNC macros :&]
[s0; &]
[s0;l160; [/ void MyEncryptedFunction(void)]&]
[s0;l160; [/ `{]&]
[s0;l160; [/ -|][*/ PROTECT`_START`_FUNC(Decrypt);]&]
[s0;l160;/ &]
[s0;l160; [/ -|<here my function code>]&]
[s0;l160;/ &]
[s0;l160; [/ -|][*/ PROTECT`_END`_FUNC;]&]
[s0;l160;/ &]
[s0;l160; [/ `}]&]
[s0;l160; &]
[s0; [*/ Important: Protected code cannot contain any references to 
global/static data or functions. Following code will likely crash 
if either of commented lines is present:]&]
[s0; &]
[s0;l160; [/ int global`_variable;]&]
[s0;l160;/ &]
[s0;l160; [/ void SomeFunction();]&]
[s0;l160;/ &]
[s0;l160; [/ void MyEncryptedFunctionThatCrashes(void)]&]
[s0;l160; [/ `{]&]
[s0;l160; [/ -|static int static`_variable;]&]
[s0;l160;/ &]
[s0;l160; [/ -|][*/ PROTECT`_START`_FUNC(Decrypt);]&]
[s0;l160;/ &]
[s0;l160; [/ -|void (`*fn) `= SomeFunction(); // reference to function]&]
[s0;l160; [/ -|int a `= global`_variable; // reference to global variable]&]
[s0;l160; [/ -|a `= static`_variable; // reference to static variable]&]
[s0;l160; [/ -|printf(`"Hello world`"); // literal constant references 
global data]&]
[s0;l160;/ &]
[s0;l160; [/ -|][*/ PROTECT`_END`_FUNC;]&]
[s0;l160;/ &]
[s0;l160; [/ `}]&]
[s0; &]
[s0; &]
[s0; &]
[s0; Define a decryption function ([* Decrypt ]in this case) which 
obtains by any mean you choose the decryption key and do the 
decryption process; for this purpose, Protect package gives a 
function that does the decryption, named [* PROTECT`_DECRYPT()] 
; following dumb example simply hard`-codes the decryption key 
:&]
[s0; &]
[s0;l128; [/ void Decrypt(byte `*start, size`_t len, byte const `*nonce, 
size`_t nonceLen)]&]
[s0;l128; [/ `{]&]
[s0;l128; [/ -|return ][*/ PROTECT`_DECRYPT ][/ ( start, len, `"`\xAA`\xBB`\xCC`\xDD`\xEE`\x
FF`", nonce, nonceLen);]&]
[s0;l128; [/ `}]&]
[s0;l128; &]
[s0; In a real app, the key can be read from a dongle, from a license 
file, from internet or generated by a combination of machine 
identification number and a license key.&]
[s0; &]
[s0; You can also check for key correctness on app startup, and abort 
the app if key is incorrect; the macro [* ON`_PROTECT`_BAD`_KEY(decrFunc)] 
executes a block of code only if the key isn`'t correct. Here 
an example usage :&]
[s0; &]
[s0;l128; [*/ ON`_PROTECT`_BAD`_KEY(decrFunc)]&]
[s0;l128; [/ `{]&]
[s0;l128; [/ -|PromptOK(`"License error`");]&]
[s0;l128; [/ -|exit(1);]&]
[s0;l128; [/ `}]&]
[s0;l128; &]
[s0; That`'s all ! If you build your application with TheIde, selecting 
the [* GUI PROTECT ]main configuration, it`'ll be automatically 
built and encrypted with the key you`'ve chosen.&]
[s0; You can see all that working in ProtectTest demo application; 
if you change the key in its [* GetKey()] function your app will 
signal the license error upon run, giving the user the choice 
of exiting the app or continue anyways. Of course, if you choose 
to continue, the app will crash.&]
[s0; If the key is correct, no alert is shown and the app runs normally.&]
[s0; &]
[s0; A note : the macros [* PROTECT`_START`_FUNC()] and [* PROTECT`_END`_FUNC] 
creates an internal block of code enclosed by a couple of `{`}, 
for technical reasons. The caveat is that [*/ any][*  ]variable defined 
[*/ inside ]the macro couple will not be visible from the outside. 
So :&]
[s0; &]
[s0;l160; [/ -|][*/ PROTECT`_START`_FUNC(Decrypt);]&]
[s0;l160; [/ -|int i `= 10;]&]
[s0;l160; [/ -|][*/ PROTECT`_END`_FUNC;]&]
[s0;l160; [*/ -|][/ return i;]&]
[s0; &]
[s0; Will give an error because of undefined variable i; the solution 
is to put variable definition [/ before ][* PROTECT`_START`_FUNC] 
or the return statement [/ inside ]the macro couple :&]
[s0; &]
[s0;l160; [/ -|int i `= 10;]&]
[s0;l160; [/ -|][*/ PROTECT`_START`_FUNC(Decrypt);]&]
[s0;l160; [/ -|][*/ PROTECT`_END`_FUNC;]&]
[s0;l160; [*/ -|][/ return i;]&]
[s0; &]
[s0; or&]
[s0; &]
[s0;l160; [/ -|][*/ PROTECT`_START`_FUNC(Decrypt);]&]
[s0;l160; [/ -|int i `= 10;]&]
[s0;l160; [*/ -|][/ return i;]&]
[s0;l160; [/ -|][*/ PROTECT`_END`_FUNC;]&]
[s0; &]
[s0; A note : [* PROTECT`_START`_FUNC ]encryption [* IS ]thread safe, 
so you can encrypt multithreaded routines.&]
[s0; &]
[s0; On next chapter we`'ll introduce another kind of copy protection 
tool named [^topic`:`/`/Protect`/srcdoc`/Obfuscation`$en`-us^ obfuscation].]]