Search code examples
androiddelphidelphi-10-seattle

using serial number security in android app using delphi


This code is working in a Firemonkey Windows app, but doesn't work in Android app, i get Goodbye instead of Welcome, what is wrong?

Edit8 Text : 162496 //Computer unique code

Edit9 Text : 1564224593 //serial #

  procedure TForm2.Button5Click(Sender: TObject);
  var
   f2,f1:textfile;
   i,j:byte;
   s1,s2,s3,c:string;
   F: TextFile;
  begin
    j:=0;
    s2 := Edit8.Text;

    for i:=1 to  Length(s2) do
    if (s2[i]>='0') and (s2[i]<='9') then
    s3:=s3+s2[i];

    for i:=1 to  Length(s3)-1 do
    if (edit9.Text[i*2-1]<>s3[i]) or (abs(strtoint(s3[i+1])-strtoint(s3[i]))<> strtoint(edit9.Text[i*2])) then 
    inc(j);

    if j=0 then
      ShowMessage('Welcome')
    else
       ShowMessage('Goodbye');
  end;

Solution

  • Delphi mobile compilers use zero-based strings.

    You have three choices:

    1. As @Günter_the_Beautiful points out, your best choice is to rewrite your code to use string helpers (which are always 0-based)
    2. Rewrite your code to use 0-based indexing: for I := 0 to ...
    3. If you need a quick fix, turn it off locally for your code snippet using the {$ZEROBASEDSTRINGS OFF} directive (and revert it back with {$ZEROBASEDSTRINGS ON} again).

    For options 2. and 3., if you need your code to be cross-platform, consider using appropriate platform conditional defines. This is what makes option 1. compelling: no need to clutter your code with conditional defines.