iOS Question encrypt in b4a,but not decrypted in b4i

watesoft

Active Member
Licensed User
Longtime User
I use below code to encrypt and decrypt file in B4A,all is fine:
Encrypt and decrypt file in B4A:
Sub EncryptFile (Path As String, Password As String)
    Dim size As Long = File.Size(Path, "")
    Dim rafInput, rafOutput As RandomAccessFile
    rafInput.Initialize(Path, "", True)
    rafOutput.Initialize(Path & ".enc", "", False)
    Dim enc As B4XCipher
    Dim buffer(2024 * 1024) As Byte
    Do While rafInput.CurrentPosition < size
        Dim count As Int = rafInput.ReadBytes(buffer, 0, Min(buffer.Length, size - rafInput.CurrentPosition), rafInput.CurrentPosition)
        Dim b() As Byte
        If count = buffer.Length Then
            b = buffer
        Else
            Dim b(count) As Byte
            Bit.ArrayCopy(buffer, 0, b, 0, count)
        End If
        Dim bb() As Byte = enc.Encrypt(b, Password)
        rafOutput.WriteInt(bb.Length, rafOutput.CurrentPosition)
        rafOutput.WriteBytes(bb, 0, bb.Length, rafOutput.CurrentPosition)
        Sleep(30)
    Loop
    rafInput.Close
    rafOutput.Close
End Sub

Sub DecryptFile (Path As String, Password As String)
    Dim size As Long = File.Size(Path, "")
    Dim rafInput, rafOutput As RandomAccessFile
    rafInput.Initialize(Path, "", True)
    rafOutput.Initialize(Path & ".decrypted", "", False)
    Dim b() As Byte
    Dim enc As B4XCipher
    Do While rafInput.CurrentPosition < size
        Dim blockLength As Int = rafInput.ReadInt(rafInput.CurrentPosition)
        If b.Length <> blockLength Then
            Dim b(blockLength) As Byte
        End If
        rafInput.ReadBytes(b, 0, b.Length, rafInput.CurrentPosition)
        Dim bb() As Byte = enc.Decrypt(b, Password)
        rafOutput.WriteBytes(bb, 0, bb.Length, rafOutput.CurrentPosition)
        Sleep(30)
    Loop
    rafInput.Close
    rafOutput.Close
End Sub

In b4i, I Decrypt one file Encrypted in B4A,it not work and has no error.

Decrypt in B4i:
Sub DecryptFile(Path As String, Password As String)

    Dim size As Long = File.Size(Path, "")
    Dim rafInput, rafOutput As RandomAccessFile
    rafInput.Initialize(Path, "", True)
    rafOutput.Initialize(Path & ".decrypted", "", False)
    Dim b() As Byte
    Dim enc As Cipher
    Do While rafInput.CurrentPosition < size
        Dim blockLength As Int = rafInput.ReadInt(rafInput.CurrentPosition)
        If b.Length <> blockLength Then
            Dim b(blockLength) As Byte
        End If
        rafInput.ReadBytes(b, 0, b.Length, rafInput.CurrentPosition)
        Dim bb() As Byte = enc.Decrypt(b, Password)
        rafOutput.WriteBytes(bb, 0, bb.Length, rafOutput.CurrentPosition)
        Sleep(30)

    Loop
    rafInput.Close
    rafOutput.Close
End Sub

I have encrypted the files in B4A and applied them to the project for some time now. I tried to use encrypted files in B4I(I only change "Dim enc As B4XCipher" to "Dim enc As Cipher"), but it seems that the above decryption method is not feasible. How can I modify the decryption method in B4i?
 
Solution
Also have a look at this:

Thread 'MQTT adds extra characters when encrypted' https://www.b4x.com/android/forum/threads/mqtt-adds-extra-characters-when-encrypted.109855/
Hi hatzisn,Thanks to your advice, I solved the problem, the method is here

1753061566809.png

hatzisn

Expert
Licensed User
Longtime User
If I were you, I would encrypt the same file both in b4i and b4a and then I would load both in a Hex Editor and compare them.
 
Upvote 0

hatzisn

Expert
Licensed User
Longtime User
Upvote 0
Top