iOS Question Question about encryption b4i

Juan Marrero

Active Member
Licensed User
Longtime User
Is it possible to read an encrypted file created in b4j in b4i? I found and example in b4j forum of creating an encrypted file (for licensing purposes), in b4a it works flawlessly. I saw some examples in b4i forum but the encryption method seems different than the one used in b4j.
 

Juan Marrero

Active Member
Licensed User
Longtime User
Start Code:

#Region Project Attributes
#MainFormWidth: 400
#MainFormHeight: 200
#AdditionalJar: bcprov-jdk15on-150
#End Region

Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private lblUser As Label
Private txtUser As TextField
Private btnCreate As Button
Private txtExpDate As TextField

Dim mDate As String
End Sub

Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Main") 'Load the layout file.
MainForm.Title = "License Creator"
MainForm.Show
txtUser.RequestFocus
End Sub

Sub btnCreate_Action
Try
If txtUser.Text <> "" Then
If txtExpDate.Text = "" Then
mDate = "0/0/0000"
Else
mDate = txtExpDate.text
End If

Dim licFile As RandomAccessFile
Dim licList As List
Dim str As String

licFile.Initialize(File.DirApp , "RemSysLic.lic", False)
licList.Initialize

str = txtUser.Text & ":" & mDate

licList.Add(str)

licFile.WriteEncryptedObject(licList, "xxxxxxxxxx", licFile.CurrentPosition)

licFile.close

Dim msg As Msgboxes

msg.Show("License Created In " & File.DirApp, "")
txtUser.Text = ""
txtExpDate.Text = ""
txtUser.RequestFocus
Else
Dim msg As Msgboxes

msg.Show("User Field Cannot Be Empty", "")
txtUser.RequestFocus
End If
Catch
Dim msg As Msgboxes

msg.Show(LastException.Message, "")
End Try
End Sub

End Code
 
Upvote 0

Juan Marrero

Active Member
Licensed User
Longtime User
Ok, I'm having issues with this, don't know what I'm doing wrong. What i did in b4j was: 1) get the string that I want to encrypt, 2) convert it to bytes, 3) encrypt it, 4) convert it to string and 5) write it to text file using WriteString function.
In b4i this is what a did: 1) Use ReadString to read the string in the text file, 2) convert it to bytes, 3) decrypt it, 4) convert it to string and 5) split the string so i can read the values separately.
The issue with this code is that when the code enters the decrypt function, it just hangs. Any help will be appreciated.

This is the code in b4j to generate encrypted file to use as an encrypted license in b4i
B4X:
       Dim bc As ByteConverter
       Dim c As B4XCipher
       Dim str As String
       Dim data() As Byte
       Dim encryption() As Byte
       Dim msg As Msgboxes
       
       If txtExpDate.Text = "" Then
         mDate = "0/0/0000"
       Else
         mDate = txtExpDate.text
       End If
         
       str = txtUser.Text & "," & txtIMEI.Text & "," & mDate
       data = bc.StringToBytes(str, "UTF-8")
       encryption = c.Encrypt(data, "xxxxxxxxxxxxxxxx")

       
       File.MakeDir(File.DirApp, "b4i\" & txtIMEI.Text)
       File.WriteString(File.DirApp & "\b4i\" & txtIMEI.Text, "RemSysLic.lic", bc.StringFromBytes(encryption, "UTF-8"))
       msg.Show("License created in folder " & File.DirApp & "\b4i\" & txtIMEI.Text & ".", "")

And this is the code in b4i
B4X:
       Dim bc As ByteConverter
       Dim c As Cipher
       Dim strng As String
       Dim data() As Byte
       Dim decryption() As Byte

       decryption = bc.StringToBytes(File.ReadString(File.DirDocuments, "RemSysLic.lic"), "UTF-8")
       data = c.Decrypt(decryption, "xxxxxxxxxxxxxxxx")
       strng = bc.StringFromBytes(data, "UTF-8")
       
       str = Regex.Split(",", strng)
 
Upvote 0

Juan Marrero

Active Member
Licensed User
Longtime User
This is my code now. Same as in b4a. In b4a works OK, but in b4i it hangs in "decrypt = c.decrypt(data, "xxxxxxxxxxxxxxxx")" line. No error, it just hangs.

B4X:
Try
       Dim bc As ByteConverter
       Dim c As Cipher
       Dim data() As Byte
       Dim decrypt() As Byte
       Dim strng As String = File.ReadString(AppPath, "RemSysLic.lic")
       Dim decryptedData As String

       data = bc.HexToBytes(strng)
       decrypt = c.decrypt(data, "xxxxxxxxxxxxxxxx")
       decryptedData = bc.StringFromBytes(decrypt, "UTF-8")
       
       LICENSE = decryptedData
       
       str = Regex.Split(",", LICENSE)
Catch
       Msgbox(LastException.Description, "")
End Try
 
Upvote 0

Juan Marrero

Active Member
Licensed User
Longtime User
This is the b4j code where I generated the encrypted file:

B4X:
Try
       Dim bc As ByteConverter
       Dim c As B4XCipher
       Dim str As String
       Dim data() As Byte
       Dim encryption() As Byte
       Dim msg As Msgboxes
       
       If txtExpDate.Text = "" Then
         mDate = "0/0/0000"
       Else
         mDate = txtExpDate.text
       End If
         
       str = txtUser.Text & "," & txtIMEI.Text & "," & mDate
       data = bc.StringToBytes(str, "UTF-8")
       encryption = c.Encrypt(data, "xxxxxxxxxxxxxxxx")

       File.MakeDir(File.DirApp, "b4i\" & txtIMEI.Text)
       File.WriteString(File.DirApp & "\b4i\" & txtIMEI.Text, "RemSysLic.lic", bc.HexFromBytes(encryption))
       msg.Show("License created in folder " & File.DirApp & "\b4i\" & txtIMEI.Text & ".", "")
Catch
       Dim msg As Msgboxes
       
       msg.Show(LastException.Message, "")
End Try
 
Upvote 0

Juan Marrero

Active Member
Licensed User
Longtime User
No. I still have v1.00. I'm trying to look for it and for some reason the search engine is failing :/ and in the documentation there is no link to download it (BTW it still says v1.00 in Documentation/Libraries.
 
Upvote 0

Juan Marrero

Active Member
Licensed User
Longtime User
Updated to v1.01 and does the same thing on c.decrypt line, it just hangs. :(

Just a comment, I'm using the latest update from bouncy castle #AdditionalJar: bcprov-jdk15on-151, the example uses bcprov-jdk15on-150. Not sure if that is creating compatibility issues here.
 
Upvote 0
Top