Android Question How to convert code VB6 to b4a?

watesoft

Active Member
Licensed User
Longtime User
This is a EnDeCryption module which I writed in vb6, now I want to convert it to b4a code, I try to do it ,but there are many errors.

Old VB6 code:

Private SubKey() As Byte
Private m_Key() As Byte
Private m_KeyValue As String
Private m_KeyLen As Long

'Generate SubKey
Private Sub GenKey(ByVal MainKey As Long, ByVal Times As Integer)
ReDim SubKey(Times, 4) As Byte
Dim i As Integer, j As Integer
SubKey(0, 0) = MainKey And &HFF&
SubKey(0, 1) = (MainKey And &HFF00&) \ &H100&
SubKey(0, 2) = (MainKey And &HFF0000) \ &H10000
SubKey(0, 3) = (MainKey And &H7F000000) \ &H1000000 - 128 * (MainKey < 0)
For i = 1 To Times - 1
For j = 1 To 3
SubKey(i, 0) = SubKey(i, 0) Xor ((i * 10) Xor (j * 10))
SubKey(i, j) = ByteAdd(SubKey(i - 1, j - 1), SubKey(i, j)) Xor ((10 * i) Xor j)
Next
Next
For i = 0 To 3
SubKey(Times - 1, i) = SubKey(Times - 1, i) Xor (i * 75)
Next
For i = Times - 2 To 0 Step -1
For j = 2 To 0 Step -1
SubKey(0, j) = SubKey(0, j) Xor ((i * 5) Xor (j * 5))
SubKey(i, j) = ByteAdd(SubKey(i + 1, j + 1), SubKey(i, j)) Xor ((10 * i) Xor j)
Next
Next
End Sub

'ByteAdd
Function ByteAdd(Byte1 As Byte, Byte2 As Byte) As Byte
If CInt(Byte1) + CInt(Byte2) > 255 Then
ByteAdd = CInt(Byte1) + CInt(Byte2) - 256
Else
ByteAdd = (CInt(Byte1) + CInt(Byte2)) Mod 256
End If
End Function

'ByteMinus
Function ByteMinus(Byte1 As Byte, Byte2 As Byte) As Byte
If (CInt(Byte1) - CInt(Byte2)) < 0 Then
ByteMinus = (CInt(Byte1) + 256) - CInt(Byte2)
Else
ByteMinus = CInt(Byte1) - CInt(Byte2)
End If
End Function

'Encryption
Public Function EnCrypt(StrIn As String, Key As Long, Times As Integer) As String
Dim StrByte() As Byte, i As Integer, j As Integer, n As Integer, k As Integer
If Len(StrIn) <= 0 Then
MsgBox "StrIn is invalid!", vbExclamation + vbOKOnly, "Encryption"
Exit Function
End If
If Key < 32767 Or Key > 2147483647 Or IsNumeric(Key) = False Then
MsgBox "Key is invalid!", vbExclamation + vbOKOnly, "Encryption"
Exit Function
End If
If Times < 0 Or Times >= 20 Or IsNumeric(Times) = False Then
MsgBox "Times is invalid!"
Exit Function
End If

Call GenKey(Key, Times)
StrByte() = StrIn
n = UBound(StrByte)
For k = 0 To Times - 1
'For j = 0 To Int(n / 2)
For j = 0 To 3
StrByte(0) = ByteAdd(StrByte(0), SubKey(k, 2))
For i = 1 To n
StrByte(i) = ByteAdd(StrByte(i - 1), StrByte(i)) Xor SubKey(k, 0)
Next
StrByte(n) = ByteAdd(StrByte(n), SubKey(k, 3))
For i = n - 1 To 0 Step -1
StrByte(i) = ByteAdd(StrByte(i + 1), StrByte(i)) Xor SubKey(k, 1)
Next
Next
Next
EnCrypt = StrByte()
Erase SubKey()
Erase StrByte()
End Function

'Decryption
Public Function DeCrypt(StrIn As String, Key As Long, Times As Integer) As String
Dim StrByte() As Byte, i As Integer, j As Integer, n As Integer, k As Integer
If Len(StrIn) <= 0 Then
MsgBox "StrIn is invalid!", vbExclamation + vbOKOnly, "Decryption"
Exit Function
End If
If Key < 32767 Or Key > 2147483647 Or IsNumeric(Key) = False Then
MsgBox "Key is invalid!", vbExclamation + vbOKOnly, "Decryption"
Exit Function
End If
If Times < 0 Or Times >= 20 Or IsNumeric(Times) = False Then
MsgBox "Times is invalid!"
Exit Function
End If
Call GenKey(Key, Times)

StrByte() = StrIn
n = UBound(StrByte)

For k = Times - 1 To 0 Step -1
'For j = 0 To Int(n / 2)
For j = 0 To 3
For i = 0 To n - 1
StrByte(i) = ByteMinus((StrByte(i) Xor SubKey(k, 1)), StrByte(i + 1))
Next
StrByte(n) = ByteMinus(StrByte(n), SubKey(k, 3))
For i = n To 1 Step -1
StrByte(i) = ByteMinus((StrByte(i) Xor SubKey(k, 0)), StrByte(i - 1))
Next
StrByte(0) = ByteMinus(StrByte(0), SubKey(k, 2))
Next
Next
DeCrypt = StrByte()
Erase SubKey()
Erase StrByte()
End Function

Who can help me? many thanks
 

watesoft

Active Member
Licensed User
Longtime User
I convert it myself, but the result of Decryption is wrong. anyone help me?
 

Attachments

  • EnDnCrypt.zip
    40.6 KB · Views: 161
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
Why aren't you switching to a "real" encryption method? It will be simple and more secure. You can use Encryption library or B4XCipher which is cross platform compatible.

Hello Erel,thanks for your advice. This is just my interest,I converted it to vb6 from Delphi a few years ago, now I also want to write it with B4A code.
In addition, I have a favor to ask:
I download the latest version html-textview 2.0 code from GitHub<https://github.com/SufficientlySecure/html-textview>, Now I use the windows OS and don't want to instll the Linux OS. If you are free and have a Compile environment, can you help me to build it? thank you very much.
Best regards
 

Attachments

  • html-textview-master.zip
    118.4 KB · Views: 155
Upvote 0
Top