Tool Secured Assets - Encrypt your assets files

This tool allows you to encrypt a set of files on the desktop and then add them to your project instead of the regular assets files. The problem with regular assets files is that it is very simple to extract them from the APK file as the APK file is just a standard zip file.

SS-2013-12-11_11.24.37.png



In the B4A program we need to unpack and decrypt the files. This is only done once. Unfortunately this step (deciphering) is quite slow. You should only use this solution with small files. It will be too slow for larger files.

The sub signature is:
B4X:
Private Sub UnpackEncryptedAssets(StoreName As String, Password As String, Version As Int)
The StoreName is the name of the database file.
Whenever you modify the store files you need to increase the version. Otherwise the store will not be unpacked. You can see the code in the attached example.

How to use

- Create the store file with the desktop tool and add it to the Files tab.
- Make sure that the Files folder only includes files that are not in the store. Remember that all files in this folder will be added to the APK.
- Add the UnpackEncryptedAssets sub, KeyValueStore class and the following libraries: SQL, RandomAccessFile.
- Call UnpackEncryptedAssets in your main activity when FirstTime is True.
- You will need to use Main.UpdatedAssetsFolder instead of File.DirAssets to access the unpacked files.
- The files names are case sensitive and are all lower cased. You should change the existing references accordingly.
- Make sure to use a process global variable for the password. The string will be obfuscated.

This tool is a bit of an overkill in most cases. A hacker with a rooted device can fetch the files from the internal folder after they are unpacked.

You can modify the code to delete the files after they are used. In that case you should also delete store.db that is created in the "version folder" (see the code).

The compiled tool can be downloaded here: www.b4x.com/b4j/files/SecuredAssets.jar
To build the B4J tool you will need to download the bouncy castle library: Share encrypted data with B4A
 

Attachments

  • B4J-SecuredAssets.zip
    4 KB · Views: 740
  • Asteroids_Encrypted.zip
    311.4 KB · Views: 817

peacemaker

Expert
Licensed User
Longtime User
Ha ! Recently it was asked...
So, direct decription to the memory from the encrypted DB or file - still impossible ...
 

wimpie3

Well-Known Member
Licensed User
Longtime User
Hmmm... couldn't it be possible for B4A to encrypt the files when you compile the project instead of using an external program? And indeed peacemaker, using the images without writing them to the internal storage first would be fantastic.
 

JoanRPM

Active Member
Licensed User
Longtime User
Very interesting.
It would be better if it were intergrade in the compiler.
 

masky

Member
Licensed User
Longtime User
Hello Erel,

Is possible something similar in the opposite way? I mean…


1. Create a file into the mobile in runtime. Encrypt.

2. Export this file to the computer.

3. Decrypt.


Thank you.
 

masky

Member
Licensed User
Longtime User
Hello Erel,

and now I silly question. When I create a new txt file in my app at runtime, where can I find this into the mobile. I'm looking for the file but I don't know where the apk has been unzip and installed. Thanks.
 

Ratna Fang

Member
Licensed User
Longtime User
hi erel,

can i use this method to encrypt *.jpg (not asset files) using b4j app and then decrypt the *.jpg using android app?
using random access file lib?
 
  • Like
Reactions: omo

masky

Member
Licensed User
Longtime User
Hello Erel,

only one detail...into the UnpackEncryptedAssets sub, ProgressDialogShow("Preparing resource files...") is not showing.

Thank you.
 

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Hi.thank you
for me get error when add files but if directory is empty not error what?
 

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Erel excuse me
when i initialize database in sqlchipher get error
Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1)
and not initialize database
i create this database with SecureAssset
what am i doing?
 

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Hi Erel.
I cannot encrypt big file or SecureAsset not add password on file why?(of course small is encrypt)
 
Top