Título: Convertir Cadena Hex a Bytes y compararlos Con Bytes de un archivo
Publicado por: **Aincrad** en 13 Octubre 2018, 19:57 pm
Bueno hola foro, Como dice el titulo, tengo un Escanear Hexadecimal para identificar archivos, Pero es lento y segun me he documentado se puede mejorar si : 1) Convierto la cadena Hex a Bytes.
2) Obtengo los Bytes del archivo .
4) Comparo la Cadena Hex convertida a Bytes con los Bytes del archivo.
Bueno ahora lo que tengo hecho hasta ahora : Cadena Hex Almacenada en una variable XML:Dim xml = <?xml version="1.0"?> <signatures> <signature> <name>Archivo1</name> <hex>58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441</hex> </signature> <signature> <name>Archivo2</name> <hex>f649e7cc1e00d37e7f3bc85fff3486ac6de91433aa3a39ef1b114d37b534b8323f6ff67132638a3fe2f2afb4aaf9b7e3b4669bb3cab028298aab533c5d73546cdd396fd58c2c7734c50bca68eb709b889a086fb3db5f8ae533a4d5816e8c5f560983695efa14e291c204b1316e657773</hex> </signature> </signatures>
Aquí el código para leer el XML y Convertirlo a Bytes: Private Sub hexttoBytes() Dim Str As New StringBuilder For Each signature As XElement In xml.Root.Elements stringToByteArray(signature.<hex>.Value) Next End Sub Public Shared Function stringToByteArray(text As String) As Byte() Dim bytes As Byte() = New Byte(text.Length \ 2 - 1) {} For i As Integer = 0 To text.Length - 1 Step 2 bytes(i \ 2) = Byte.Parse(text(i).ToString() & text(i + 1).ToString(), System.Globalization.NumberStyles.HexNumber) Next Return bytes End Function
Aquí el código para Obtener los Bytes del archivo a comparar ' Opendia IS OPENFILEDIALOG Dim result As DialogResult = OpenDia.ShowDialog() If result = Windows.Forms.DialogResult.OK Then Dim path As String = OpenDia.FileName Try Dim bytesFile As Byte() = File. ReadAllBytes(path ) ' BytesFile es donde se almacenan Los Bytes del archivo a comparar ' Necesito saber Como los Comparo con los Otros Bytes que antes eran HEX Catch ex As Exception MsgBox(ex.Message) End Try End If
AHORA LAS DUDAS, SEGÚN LO QUE TENGO ECHO , No logro Como hacer Para Comparar los Bytes que antes eran HEX con los Bytes del archivo. Lo que quiero lograr seria algo como esto : 'Donde BytesFiles es el Archivo y BytesHex eran la Cadena Hex que ahora son Bytes ' En caso de que los Bytes sean Iguales. lanza el mensaje de que encontró el archivo o no. If bytesFile = BytesHex then Msgbox("Archivo Coincide") else Msgbox("Archivo No Coincide") end if
Gracias De Antemano
Título: Re: Convertir Cadena Hex a Bytes y compararlos Con Bytes de un archivo
Publicado por: Eleкtro en 13 Octubre 2018, 22:57 pm
No logro Como hacer Para Comparar los Bytes que antes eran HEX con los Bytes del archivo. Te escribo un ejemplo... Dim signature As Byte?() = {&HFF, &HD8, &HFF} ' JPEG magic number. Dim fi As New FileInfo("C:\Image.jpg") ' JPEG image. Dim chunk As Byte() = GetFileChunk(fi, 0, signature.Length) ' First three bytes (signature length) of the JPEG image. Dim result As Boolean = IsMatchSignature(chunk, signature, offset:=0) Console.WriteLine("Signature Match?: {0}", result)
Public Shared Function GetFileChunk (file As FileInfo, offset As Integer, length As Integer) As Byte() Dim fileLen As Long = file. Length If (fileLen < length) Then length = CInt(fileLen) End If Dim chunk As Byte() = New Byte(length - 1) {} Using fs As New FileStream (file. FullName, FileMode. Open, FileAccess. Read, FileShare. Read) fs.Read(chunk, offset, length) End Using Return chunk End Function
Public Shared Function IsMatchSignature(bytes As Byte(), signature As Byte?(), offset As Integer) As Boolean For i As Integer = 0 To (signature.Length - 1) ' If byte value in file signature is null, the value is variable, so we ignore it. If (signature(i) Is Nothing) Then Continue For End If If (signature(i) <> bytes(i + offset)) Then Return False End If Next i Return True End Function
Saludos.
Título: Re: Convertir Cadena Hex a Bytes y compararlos Con Bytes de un archivo
Publicado por: **Aincrad** en 13 Octubre 2018, 23:09 pm
Y como aplico ese ejemplo al mio? es que tengo una base de datos XML grande. Dim xml = <?xml version="1.0"?> <signatures> <signature> <name>Archivo1</name> <hex>58354f2150254041505b345c505a58353428505e2937434329377d2445494341522d5354414e4441</hex> </signature> <signature> <name>Archivo2</name> <hex>f649e7cc1e00d37e7f3bc85fff3486ac6de91433aa3a39ef1b114d37b534b8323f6ff67132638a3fe2f2afb4aaf9b7e3b4669bb3cab028298aab533c5d73546cdd396fd58c2c7734c50bca68eb709b889a086fb3db5f8ae533a4d5816e8c5f560983695efa14e291c204b1316e657773</hex> </signature> </signatures>
Título: Re: Convertir Cadena Hex a Bytes y compararlos Con Bytes de un archivo
Publicado por: Eleкtro en 14 Octubre 2018, 00:52 am
¿Cómo que cómo lo aplicas?, exactamente del mismo modo en el que te acabo de mostrar... pero usando tus secuencias (firmas) DEC que conviertes desde cadenas de texto HEX. Las funciones que he compartido son reutilizables, de uso genérico, copiar y listo.
|