Android Question How to add a few bytes or remove some in a binary file

JOANORSKY

Member
Licensed User
Longtime User
Hi there.. once again,
so.. i am facing a little issue again (as usual). Since i am relatively new to B4A on file manipulation i have now faced a problem that some of you have as well. I am trying to obfuscate some of my assets files by intentionally remove a few bytes from the header and on user demand i want to add those missing values again and by this way restore the original binary for usage.

So far i have came with this code :

B4X:
Sub decode_file(path_to_save As String, Original_file As String, Saved_file As String)
    Dim In As InputStream '<--------- from file to array of byte
    In = File.OpenInput(File.DirAssets, Original_file)
    Dim out As OutputStream
    out.InitializeToBytesArray(1000)
    File.Copy2(In, out) '<---- This does the copying
    Dim data() As Byte
    '-------------------------------*****START OF ADD MISSING BYTES*******--------------------------
    data = "%PDF-1.5".GetBytes("UTF8") '<---- as it seems this can't be done
    data = data & out.ToBytesArray '<---- as it seems this can't be done
    '-------------------------------*****END OF ADDING MISSING BYTES*******--------------------------
    out.Flush '<---- Flushing of the buffer in order to be used again
    '--------- Now it is being converted from array of byte to file again
    out =  File.OpenOutput(path_to_save, Saved_file, False)
    out.WriteBytes(data, 0, data.Length)
    out.Close
    In.Close
End Sub

In order to remove a few random bytes from the reading that i might not want there i just need to replace the code as follows :
B4X:
out.WriteBytes(data, 0, data.Length)

by

B4X:
out.WriteBytes(data, Start_index, data.Length-Ending-Index)

But.. how do i add a few bytes in the start of the array?

I was trying to do this :
B4X:
    '-------------------------------************--------------------------
    data = "%PDF-1.5".GetBytes("UTF8") '<---- as it seems this can't be done
    data = data & out.ToBytesArray '<---- as it seems this can't be done
    '-------------------------------************--------------------------

.. in order to add "%PDF-1.5" bytes to the start of the buffer (data) as if a string manipulation but i don't really know how to combine two arrays of bytes at the start of one specific array. I know that the code can't possibly work as is.. i mean with the "&" operator (as if a string manipulation)... but is there any homologue function for arrays of bytes or a specific library function?

Any suggestion or tip?
 
Last edited:

JOANORSKY

Member
Licensed User
Longtime User
Well i found a way to circumvent the issue. I used the ByteConverter library in order to replace a initial (start of the file which has) junk random bytes by the real ones..

What i did was to convert the byte stream to hex and then replaced the junk in hex format by the real values also in hex (basic string manipulation). Later on i re-converted the string to the appropriated byte format with HexToBytes command.

This works nicely.. and with it... i now can corrupt all resources within assets, but is not the ideal! Without the real (and missing) bytes there the files are useless if extracted. Although this seems to me as resource inefficient. A better way would be to just append two Arrays of bytes.. but.. how?! This might sound a stupid question... but i am still learning file manipulation on B4A.. so if anyone out there has some spare time to exchange some ideas.. i would appreciate it.. a lot! :)
 
Upvote 0
Top