iOS Question [solved]No response when using wrong password with b4xencryption

schimanski

Well-Known Member
Licensed User
Longtime User
I'm using the following code under B4A, B4J and B4I to encrypt and decrypt files. But now, I have mentioned, that if i use data=Cipher.Decrypt(data, Password) with a wrong password under b4i, I don't get an exception like under b4A or b4j. How can I check, if the password is wrong? In my case, the sub decryptfile decrypts the file with the wrong password and kills the file:

B4X:
encryptFile(File.DirAssets, "test1.jpg", File.DirLibrary, "crypt_test.jpg")
decryptFile(File.DirLibrary, "crypt_test.jpg", File.DirLibrary, "decrypt_test.jpg")

Sub Process_Globals

    #If B4I
        Dim Cipher As Cipher
    #Else
        Dim Cipher As B4XCipher
    #End If

    Dim su As StringUtils
    Dim bc As ByteConverter
    Dim Password As String ="blabla"
End Sub

Sub encryptFile(DirSource As String, fileSource As String, DirTarget As String, FileTarget As String)
    Dim time As Long  =DateTime.Now
   
    Dim in As InputStream
    in = File.OpenInput(DirSource, fileSource)
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    File.Copy2(in, out)

    Dim data() As Byte
    data = out.ToBytesArray
    data=Cipher.Encrypt(data, Password)
   
    Dim Dokumentdatei As RandomAccessFile
    Dokumentdatei.Initialize(DirTarget, FileTarget, False)
    Dokumentdatei.WriteB4XObject(data, 0)
    Dokumentdatei.Close
   
    Log("Verschlüsseln dauert: " & (time-DateTime.Now))
    time=DateTime.Now
End Sub

Sub decryptFile(DirSource As String, FileSource As String, DirTarget As String, FileTarget As String)
    Dim time As Long  =DateTime.Now

    Dim data() As Byte
    Dim Dokumentdatei As RandomAccessFile
    Dokumentdatei.Initialize(DirSource, FileSource, True)
    data=Dokumentdatei.ReadB4XObject(0)
    Dokumentdatei.Close
   
    data=Cipher.Decrypt(data, Password)

    Dim in As InputStream
    in.InitializeFromBytesArray(data, 0, data.Length)
    Dim out As OutputStream
    out=File.OpenOutput(DirTarget, FileTarget, False)
    File.Copy2(in, out)
    out.Close

    Log("Entschlüsseln dauert: " & (time-DateTime.Now))
    time=DateTime.Now
End Sub
 

schimanski

Well-Known Member
Licensed User
Longtime User
In both decryptions it is an bytearray. The first one is with the right password, the second with the wrong password. How is it possible to see, that the bytearray is wrong encrypted?

B4X:
<B4IArray: 0x12de82660>

B4X:
<B4IArray: 0x13de84de0>

In B4A i get an

javax.crypto.BadPaddingException: error:1e06b065:Cipher functions:EVP_DecryptFinal_ex:BAD_DECRYPT

with the wrong password. In b4i, it happens nothing.
 
Last edited:
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
In my case, it is an SQLit-database, which I encrypt and try to decrypt.

I think, I could decrypt the file under another name and try to inizialize the database. If initialize raises an exception, the password is wrong. But it is not very smooth...
 
Last edited:
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
The follwing code could check, if the file (in my case a keyvaluestore2) could be decrypted!

Thanks for help...

B4X:
decryptFile(File.DirLibrary, "test.db", File.DirLibrary, test-double.db", password)
..
Try
     Dim kvs As KeyValueStore
     kvs.Initialize(File.DirLibrary, "test-double.db")
     File.Copy(File.DirLibrary, "test-double.db", File.DirLibrary, "test.db")
     Return "success"
Catch
     Return "wrong password!"
End Try
 
Upvote 0
Top