Issue Porting XOR Encryption Code

mattn

Member
Licensed User
Longtime User
Hi,

I recently started using Basic4Android and have been trying to port a simple vb.net XOR encryption program. I have thoroughly read the topic about VB to Basic4Android however it only covers basic stuff.

The VB.net code i'm trying to port is:
B4X:
 Dim ch As Char
        Dim strCode As String
        Dim Code, Number As Short
        Dim Decrypt As String = ""

        OpenFileDialog1.Filter = "MN SecuPad Files (*.mnsec)|*.mnsec"
        OpenFileDialog1.ShowDialog()
        If OpenFileDialog1.FileName <> "" Then
            Try
                strCode = InputBox("Encryption Key:")
                If strCode = "" Then Exit Sub
                Code = CShort(strCode)
                FileOpen(1, OpenFileDialog1.FileName, OpenMode.Input)
                Do Until EOF(1)
                    Input(1, Number)
                    ch = Chr(Number Xor Code)
                    Decrypt = Decrypt & ch
                Loop
                RichTextBox1.Text = Decrypt
                RichTextBox1.Select(1, 0)
                '                MenuItem4.Enabled = False
            Catch ex As Exception
                MsgBox("Incorrect Encryption Key. Access Denied!!")
            Finally
                FileClose(1)
            End Try
        End If

Currently i've got this as the Basic4Android code:
B4X:
  Dim encfile As InputDialog
   Dim fd As FileDialog
   Dim readable As String
   Dim inputcode1 As String
   Dim inputc1 As Short
         
   fd.FastScroll = True
   fd.FilePath = File.DirInternal
   fd.FileFilter = ".mnsec" 

   ret = fd.Show("Select A File:", "OK", "Cancel", "", Null)   
   If ret = -3 OR fd.ChosenName = "" Then 
      Return
   End If
   If File.Exists(File.DirInternal, fd.ChosenName) = False Then
      Msgbox(fd.ChosenName & " does not exist.", "")
      Return
   End If
   Dim TR As TextReader
   Dim s As String
   Dim ch As Char
   Dim Number As Short
   Dim Decrypt As String
   Dim c As Cipher
   
   TR.Initialize(File.OpenInput(File.DirInternal, fd.ChosenName))
      s = TR.ReadLine
      'add decryption and display file contents code here
      encfile.Show("Enter Encryption Code","Code","OK","Canel","Negative",Null)
      inputcode1 = encfile.Input
      
      Dim Code As Short
            inputc1 = encfile.Input
            Code = inputc1
         'Code = inputcode1
            File.OpenInput(File.DirInternal,fd.ChosenName)
            'Do Until s.Length
         'Number = s
         Decrypt = inputc1 + Code
         'Loop
         Msgbox("Incorrect Encryption Key. Access Denied!!","ACCESS DENIED!!!!")
         'EditText1.Text = s
            EditText1.Text = Decrypt
         Msgbox(Decrypt,"Test")
         'EditText1.Select(1, 0)
            'MenuItem4.Enabled = False
      'asndkajsndjkas
   TR.Close

It will load the file but it won't decrypt it.

Any help would be great and very much appreciated.
 

mattn

Member
Licensed User
Longtime User
That code you mentioned is VB.NET code that use XOR which is a VB.NET function. I'm trying to port the VB.NET code to Basic4Android. The problem is that I have no idea how to convert:
B4X:
Code = CShort(strCode)

Chr(Number Xor Code)

Do Until EOF(1)
to Basic4Android code.

EOF is end of file in VB.NET
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Yes, I understood it. However as I understand it this solution might fail in some cases. It is better to convert the string to bytes and work with bytes, not characters.
B4X:
Code = CShort(strCode) 
=>
Dim Code As Short
Code = strCode

Chr(Number Xor Code)
=>
Chr(Bit.Xor(Number, Code))

Do Until EOF(1)
=>
s = File.ReadString(...)
For i = 0 To s.Length - 1
 Dim ch as Character
 ch = s.CharAt(i)
 ...
Next
 
Upvote 0

mattn

Member
Licensed User
Longtime User
Hi Erel,

Thanks for being so helpful. I get what you're saying about it not always working, its just that this porting has been frustrating me for a while. I need your help with one more thing (hopefully the last thing that needs porting):

I need to convert the data stored in the value 's' to short so that I can be compared in the XOR function.

Currently:

B4X:
s = TR.ReadLine

and

B4X:
Dim Number As Short

...
'Number = s

This is currently commented until I can get the data contained by 's' converted to Short.
 
Upvote 0

mattn

Member
Licensed User
Longtime User
Not too sure how to implement Regex.Split as the numbers are mathematically modified character addresses, hence they can vary in length depending on the numerical code entered by the user.
 
Upvote 0
Top