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
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