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: 851
  • Asteroids_Encrypted.zip
    311.4 KB · Views: 926

omidaghakhani1368

Well-Known Member
Licensed User
Longtime User
Hard to say without seeing the error. However this solution is less useful for large files as the decryption will be too slow.
No No Erel.When i encrypt file and use in basic4android,it not have password for large file but in small file have password.My file size is 80 mb
 

tsteward

Well-Known Member
Licensed User
Longtime User
Desktop app SecuredAssets.jar crashes when I try to select the source folder.
I click the button then select a folder that has 11 jpg files totaling 400kb and the app closes without warning or error.
 

tsteward

Well-Known Member
Licensed User
Longtime User
Here is the error
C:\Users\Tony\Desktop>java -jar SecuredAssets.jar
Program started.
main._listfiles (java line: 181)
java.lang.NoSuchMethodError: javafx.scene.control.CheckBoxTreeItem.setIndependen
t(Ljava/lang/Boolean;)V
at anywheresoftware.b4j.objects.TreeViewWrapper$CheckBoxTreeItemWrapper.
Initialize(TreeViewWrapper.java:157)
at b4j.example.main._listfiles(main.java:181)
at b4j.example.main._btnbrowseinput_action(main.java:101)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:93)
at anywheresoftware.b4a.BA$2.run(BA.java:159)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Sourc
e)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1908272007.run(Unk
nown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown S
ource)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1147985808.run(Unk
nown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source)
at com.sun.glass.ui.win.WinApplication$$Lambda$37/728890494.run(Unknown
Source)
at java.lang.Thread.run(Unknown Source)


C:\Users\Tony\Desktop>
 

Ricardo Bunschoten

Active Member
Licensed User
Longtime User
If i want to crypt txt files that contains urls is that also possible to add then in a db file ?

And then unpack them to use them ?

i now use this code

B4X:
    Private link1 As String = "url.txt"

B4X:
Sub btnDownload_Click
    Filelist = File.ReadList(File.DirAssets, "url.txt")
    Base = Filelist.Get(Filelist.Size - 1)
    Label1.Text = Label1.Text & CRLF
    Log(" ")
    Log("Maximum value for Cnt = " & (Filelist.Size - 2))
    Cnt = 0
    If Cnt <= Filelist.Size - 2 Then
    FN = Filelist.Get(Cnt)
    link1 = Base & FN
    Dim dd As DownloadData
    dd.Url = link1
    dd.EventName = "dd"
    dd.Target = Me
    CallSubDelayed2(DownloadService, "StartDownload", dd)
    Cnt = Cnt + 1
    End If
    End Sub
 

Ricardo Bunschoten

Active Member
Licensed User
Longtime User
What i want is that the url.txt files are crypted so tat when they unpack the apk they can not see the url inside the txt file.
 

Ricardo Bunschoten

Active Member
Licensed User
Longtime User
i did that but your sample code astroids is unpacking images blob files ?

I was not sure what the parameter was to decrypt the crypted txt files files from the db file when they are txt files

maybe i read it wrong ?
 

Ydm

Active Member
Licensed User
Longtime User
I am using version 2 keyvalue. 1.0.1 version gives the SQL error in the Android version 6.
But I get the following error:
Error occurred on line 26 (keyvaluestor A)
the java.io.IOException

in keyvalue place where this error:
B4X:
Public Sub Get(Key As String) As Object
    Dim rs As ResultSet = sql1.ExecQuery2("SELECT value FROM main WHERE key = ?", Array As String(Key))
    Dim result As Object = Null
    If rs.NextRow Then
        result = ser.ConvertBytesToObject(rs.GetBlob2(0))  '!!!!!!!!!!!!!!!!!!!!!!
    End If
    rs.Close
    Return result
End Sub
 

Ydm

Active Member
Licensed User
Longtime User
I've tried. But this time I got the following error(in the B4J):

Error occurred on line: 44 (KeyValueStore)
java.lang.RuntimeException: Cannot serialize object: java.io.BufferedInputStream@2f4951

B4X:
Public Sub PutEncrypted (Key As String, Value As Object, Password As String)
#if B4I
    Dim cipher As Cipher
#else
    Dim cipher As B4XCipher
#end if
    Put(Key, cipher.Encrypt(ser.ConvertObjectToBytes(Value), Password))   '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! 44
End Sub

I had to use that file in Main:
B4X:
kvs.PutEncrypted(ci.Text.ToLowerCase, File.OpenInput(txtFolder.Text, ci.Text), _
                txtPassword.Text)

kvs.PutEncryptedObject unavailable.
 
Top