How to protect my “Android Apk” with a Password

Gottrik

Member
Licensed User
Longtime User
With B4ppc and a Windows Mobile Device I use the following procedure:

First, I read the Device ID:

c$ = Hardware.GetDeviceID

I modify the ID:

c$ = StrRemove(c$,6,StrLength(c$)-6) 'I use the first 8 characters

I transform the ID from Hex to Dec:

Codice = n.HexToDec (c$) ' I transform from Hex to Dec

I assign the Password value (using “ k ” and “ m ”) and I transform it from Dec to Hex:

Password = n.DecToHex(Int((Codice / k ) + m ))

With a Text Edit ( Notepad.exe) I create the Pwd.txt file and I write the Password in the first line ...
I distribute my Windows Mobile application with the Pwd.txt file ( in the same directory )
When my programm starts, it searchs for Pwd.txt file ...
and reads the “ Rpwd “ string ...

FileOpen (c1,"Pwd.txt",cRead,,cASCII)
Rpwd = FileRead (c1)
FileClose (c1)

If the Rpwd value is equal to Password value , my Apk works normally
Otherwise my Apk stops :

If Rpwd <> Password Then
Msgbox ( “Please send this code : “ & Codice & “ to ........... and you will have your Password “) ‘Apk stops
Else
DatiInput.Show ‘ Apk starts
End If

Now, my questions are :

How can I follow the above procedure but with B4Android ?
Is it possible to read the Device ID ?
How can I create a file in the Android Device root ?
I would like to distribute my Android Apk and after that I will send the “Pwd.txt” file containing the correct Password for any device ...

Have you any suggestions for me?
Thanks
Gottrik
 

gjoisa

Active Member
Licensed User
Longtime User
You can get the device imei number with the help of Phone library and have the same procedure here also .
 
Upvote 0

Gottrik

Member
Licensed User
Longtime User
Thank you GJOISA , may be a solution.

But if I use “ c$ = Hardware.GetDeviceID “ with Basic4ppc , what is the equivalent instruction for Basic4Android ?
And If I use “ FileOpen(c1,”Filename”,cRead,,cASCII) with Basic4ppc, what is the equivalent instruction for Basic4Android ?

Thanks
Gottrik
 
Upvote 0

gjoisa

Active Member
Licensed User
Longtime User
Attaching here a small project . this may help you .
 

Attachments

  • Test2.zip
    5.8 KB · Views: 582
Upvote 0

metrick

Active Member
Licensed User
Longtime User
Got following error message when compile; "An error has occurred in sub:main_activity_creat(B4A Line: 28) password=Bit.ToOctalString(Floor((codid/150)+4528657583465)) java.lang. NumberFormatException: Continue?" when compile test2. Any solution? Thanks.
 
Upvote 0

tester198

Member
Licensed User
Longtime User
Got following error message when compile; "An error has occurred in sub:main_activity_creat(B4A Line: 28) password=Bit.ToOctalString(Floor((codid/150)+4528657583465)) java.lang. NumberFormatException: Continue?" when compile test2. Any solution? Thanks.

I have the same error. Compile run and Error message.

Please post a solution anyone. Thanks
 
Upvote 0

gjoisa

Active Member
Licensed User
Longtime User
there is no any problem in my computer while compiling . did you declared the variables as integer or float ? else you may do one thing . leave that converting hex string and octal string . just add some compound calculations . for eg :
did = did.SubString2(2,13)
did = Floor(did*5/8)
did = Floor(did*13/18)
did = Floor(did*3/9)
...........
...........
password = did.SubString2(2,10)

this may compile without errors .
regards
 
Upvote 0

tester198

Member
Licensed User
Longtime User
i have download your test2.zip compile it error.

I have download your test2.zip change the code if you have write. Then comes to my handy follow code:

An error has occurred in Sub: main activity_create (B4A line: 24)
did = did.SubString2(2,13) java.lang StingIndexOutOfBoundsException Continiue

What can i do?
 
Upvote 0

gjoisa

Active Member
Licensed User
Longtime User
Usually an imei (Phone.GetDeviceId) has 15 characters . Then there can not be error . Attach your project here . I will have a look .
 
Upvote 0

tester198

Member
Licensed User
Longtime User
That is the same one that you have posted.

Ok I have tested it in the Emulator it runs but if i run it on the HTC Desire it shows me an error message.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
The problem is that you have a hex formatted string in variable codid and are using it in an arithmetic expression. If there are any hex characters above 9 present it cannot be converted to a decimal number so you get an error.

Floor((codid/150)+4528657583465)

I guess the reason it works on the emulator is that whatever the emulator returuns as a device ID doesn't convert to a hex string with non0valid decimal characters.
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I'm sorry, but I just don't understand what you want to do. If you want to replicate what you are doing with Basic4ppc why don't you just use the same algorithm. In your Basic4ppc code you use

B4X:
c$ = StrRemove(c$,6,StrLength(c$)-6)
Codice = n.[COLOR="red"]HexToDec [/COLOR](c$)
and in Basic4android you use
B4X:
did=pho.GetDeviceId
did=did.SubString2(3,10)
codid=Bit.[COLOR="Red"]ToHexString[/COLOR](did)
 
Upvote 0

Gottrik

Member
Licensed User
Longtime User
Erel,Agraham,Gjoisa ...

I have not yet purchased an Android device.
Actually I test my first Android.apk only with AVD (Android Virtual Device ).
Is it possible to create, write an read a file, using the AVD ?
Have you a little example about the correct instructions to do this?

Thanks
Gottrik
 
Upvote 0
Top