hi, first, congratulations for your lenguaje B4A its amaizing.
but ned help i have the next code in VB .net you can help to change to B4A? plis
its a socket conexion with a servidor and some encodyng and decoding.
tanks.
attached the code "CONFIGURATION.txt"
but ned help i have the next code in VB .net you can help to change to B4A? plis
its a socket conexion with a servidor and some encodyng and decoding.
tanks.
attached the code "CONFIGURATION.txt"
B4X:
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Net
Imports System.Net.Sockets
Imports System.Security.Cryptography
Imports System.Runtime.InteropServices
Imports System.IO
Public Class CONFIGURACION
Private Sub GUARDAR_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GUARDAR.Click
Modulo1.num_suc = TextBox1.Text
Modulo1.caja = TextBox2.Text
Modulo1.Id_Hardware = TextBox3.Text
Modulo1.usuario = TextBox4.Text
Modulo1.Descripcion = TextBox5.Text
Modulo1.Limite = TextBox6.Text
Modulo1.ippppp = TextBox7.Text
Try
Dim Intru As Object
Dim Archivo As Object
Intru = CreateObject("Scripting.FileSystemObject")
Archivo = Intru.CreateTextFile("C:\Ruta.txt", True)
Archivo.WriteLine(TextBox1.Text)
Archivo.WriteLine(TextBox2.Text)
Archivo.WriteLine(TextBox3.Text)
Archivo.WriteLine(TextBox4.Text)
Archivo.WriteLine(TextBox5.Text)
Archivo.WriteLine(TextBox6.Text)
Archivo.Close()
MessageBox.Show("Escritura realizada con éxito")
Catch ex As Exception
MessageBox.Show("Escritura realizada incorrectamente")
End Try
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles alta.Click
'Declaro la cadena sin la longitud, tampoco lleva la bandera que indica si se encripta o no
'eso se hace en los siguiente pasos
Dim mensaje As String = ("1:11;2:" & Modulo1.num_suc & ";3:" & Modulo1.caja & ";4:" & Modulo1.Id_Hardware & ";5:" & Modulo1.usuario & ";6:VF;13:" & Modulo1.Descripcion & ";14:" & Modulo1.Limite)
'Indica que el mensaje si va encriptado
Dim encriptar() As Byte = Encoding.ASCII.GetBytes("S")
'delcaro mi entero que almacena la longitud de mi cadena encriptada.
Dim longitud As Integer = Encrypt(mensaje).Length
'encripta el mensaje
Dim SendBytes() As Byte = Encrypt(mensaje)
'agrego un arreglo de bytes para enviarlo al socket
'No hay que convertir el entero a string (ej. el 0 ascii es el entero 48)
'tambien hay que usar IPAddress.HostToNetworkOrder
'ya que microsoft utiliza un orden de bytes little-endian y java utiliza el big-endian
'(ej. el 987 en un endian puede ser 620560384 en otro endian)
Dim SendBytesLongitud() As Byte = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(longitud))
'Para leer la longitud de respuesta
Dim RecvBytesLen() As Byte = New Byte((4) - 1) {}
Dim ipaddr As IPAddress = System.Net.IPAddress.Parse("192.168.1.69")
Dim Ep As IPEndPoint = New IPEndPoint(ipaddr, 9003)
Dim socket As Socket = New Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)
Try
socket.Connect(Ep)
'env�o primero la longitude la cadena
socket.Send(SendBytesLongitud, 0, SendBytesLongitud.Length, SocketFlags.None)
'enviar si hay que encriptar
socket.Send(encriptar, 0, encriptar.Length, SocketFlags.None)
'una vez enviada la longitud, env�o la cadena
socket.Send(SendBytes, 0, SendBytes.Length, SocketFlags.None)
'Recibe la longitud de la respuesta
socket.Receive(RecvBytesLen, RecvBytesLen.Length, SocketFlags.None)
'hace la coversion de los bytes de big-endian a little-endian
Dim lenlittle() As Byte = ReverseBytes(RecvBytesLen)
'Recerva el espacio para recibir el mensaje de respuesta
Dim RecvBytes() As Byte = New Byte((BitConverter.ToInt32(lenlittle, 0)) - 1) {}
'Recibe el mensaje
socket.Receive(RecvBytes, RecvBytes.Length, SocketFlags.None)
'Desencripta el mensaje
Dim decBytes() As Byte = DeCrypt(RecvBytes)
'Convierte los bytes del mensaje desencriptado a string
Dim respuesta As String = Encoding.ASCII.GetString(decBytes, 0, decBytes.Length)
'Muestra la respuesta en la consola
RESPALTA.Text = respuesta
Catch error1 As Exception
RESPALTA.Text = "Ha ocurrido un error: " + error1.Message
End Try
End Sub
Private Function ReverseBytes(ByVal inArray() As Byte) As Byte()
Dim temp As Byte
Dim highCtr As Integer = (inArray.Length - 1)
Dim ctr As Integer = 0
Do While (ctr _
< (inArray.Length / 2))
temp = inArray(ctr)
inArray(ctr) = inArray(highCtr)
inArray(highCtr) = temp
highCtr = (highCtr - 1)
ctr = (ctr + 1)
Loop
Return inArray
End Function
Public Function Encrypt(ByVal plainText As String) As Byte()
Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider
des.Mode = CipherMode.ECB
'esta es la llave de myairtimer
des.Key = Encoding.ASCII.GetBytes("jocelin")
des.IV = des.Key
Dim encryptor As ICryptoTransform = des.CreateEncryptor
Dim x() As Byte = UTF8Encoding.UTF8.GetBytes(plainText)
Dim enc() As Byte = encryptor.TransformFinalBlock(x, 0, x.Length)
Return enc
End Function
Public Function DeCrypt(ByVal encrytedBytes As Byte()) As Byte()
Dim des As DESCryptoServiceProvider = New DESCryptoServiceProvider
des.Mode = CipherMode.ECB
'esta es la llave de myairtimer
des.Key = Encoding.ASCII.GetBytes("jocelin")
des.IV = des.Key
Dim decryptor As ICryptoTransform = des.CreateDecryptor
Dim dec() As Byte = decryptor.TransformFinalBlock(encrytedBytes, 0, encrytedBytes.Length)
Return dec
End Function
Private Sub SalirToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SalirToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub CONFIGURACION_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
End Sub
End Class