Android Question Shifting every bit in a file

ernschd

Active Member
Licensed User
Longtime User
Because there is no library for encryption of zip files, my idea was to load the file to an Byte-Array and shift every bit to right. So the information is no more readable.
For "decryption" i have to shift the bits back to left.
May this be working?

This is my first try:
B4X:
Sub ReadFile(Dir As String, FileName As String) As Byte()
    Dim out As OutputStream
    out.InitializeToBytesArray(100) 'size not really important
    File.Copy2(File.OpenInput(Dir, FileName), out)
    Return out.ToBytesArray
End Sub

Sub writeFile(Dir As String, FileName As String, datei() As Byte)
   Dim out As OutputStream = File.OpenOutput(Dir, FileName, False)
   out.WriteBytes(datei, 0, datei.Length)
   out.Flush
   out.Close
End Sub

...

' reading the zip-file
Dim ba() As Byte   
ba = ReadFile(File.DirRootExternal, "myFile.zip")

' encryption
For i = 0 To ba.Length - 1
    ba(i) = Bit.ShiftLeft(ba(i), 1)
Next

' saving
writeFile(File.DirRootExternal, "myEnFile.zip")

' reading the new file
Dim ba() As Byte
ba = ReadFile(File.DirRootExternal, "myEnFile.zip")

' decryption
For i = 0 To ba.Length - 1
     ba(i) = Bit.ShiftRight(Bit.AND(ba(i), 0xff),1)
Next

But some bytes in the new file are wrong. I guess the problem is that the byte value range is from -128 to 127, but my values have to be unsigned.

So where's my error (it's absolutely time for the weekend ;))?

Thanks.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
guess the problem is that the byte value range is from -128 to 127, but my values have to be unsigned.
This is not the issue. It doesn't really matter whether the bytes are signed or unsigned.

Your code is incorrect. You are losing information when you call ShiftLeft or ShiftRight (the first or last bit).

For a very simple "obfuscation" you can XOR every bit.
 
Upvote 0
Top