Foro de elhacker.net

Programación => Programación Visual Basic => Mensaje iniciado por: x64core en 21 Octubre 2011, 05:16 am



Título: [Sources Code] RotBits [Byte TO Byte ]
Publicado por: x64core en 21 Octubre 2011, 05:16 am
primeramente le doy gracias a BlackZeroX (Astaroth) por haberme enseñado a manipular los bits y bytes y las famosas
mascaras de bytes por haberme tenido paciencia cuando no entendia :xD gracias tio ;D
aqui esta un poco de lo que ya sé  >:D

lo que hace este trozo de codigo es invertir cada byte del valor insertado acepta de Tipo Byte, Integer, Long.
hace la rotacion Byte por Byte...

Código
  1.  
  2. ' DWORD = LONG... ?
  3. ' HIGH/LOW DWORD
  4. Function HLDWORD(DWord As Long) As Long
  5. Dim RotHWord    As Integer
  6. Dim RotLWord    As Integer
  7.  
  8. RotHWord = HLWORD( _
  9.                        HWord(DWord) _
  10.                  )
  11.  
  12.  
  13. RotLWord = HLWORD( _
  14.                        LWord(DWord) _
  15.                  )
  16.  
  17. HLDWORD = MakeDWord(RotHWord, RotLWord)
  18. End Function
  19.  
  20. ' HIGH/LOW WORD
  21. Function HLWORD(Word As Integer) As Integer
  22. Dim RotHbyte    As Byte
  23. Dim RotLbyte    As Byte
  24.  
  25. RotHbyte = hByte(Word): RotHbyte = RotBits(RotHbyte)
  26. RotLbyte = LByte(Word): RotLbyte = RotBits(RotLbyte)
  27. HLWORD = MakeWord(RotHbyte, RotLbyte)
  28. End Function
  29.  
  30. Function HWord(DWord As Long) As Integer
  31.    HWord = (DWord / &H10000)
  32. End Function
  33.  
  34.  
  35. Function LWord(DWord As Long) As Integer
  36.    If DWord And &H8000& Then
  37.        LWord = &H8000 Or (DWord And &H7FFF&)
  38.    Else
  39.        LWord = DWord And &HFFFF&
  40.    End If
  41. End Function
  42.  
  43. Function hByte(Word As Integer) As Byte
  44.    If Word And &H8000 Then
  45.        If Not (Word Or &HFF) = &HFFFFFFFF Then Word = (Word Xor &HFF)
  46.        hByte = &H80 Or ((Word And &H7FFF) \ &HFF)
  47.    Else
  48.        hByte = Word \ 256
  49.    End If
  50. End Function
  51.  
  52. Function LByte(Word As Integer) As Byte
  53.  LByte = (Word And &HFF)
  54. End Function
  55.  
  56. ' 1 byte, Rotbits
  57. Function RotBits(XByte As Byte) As Byte
  58. Dim BytesTmp    As Byte
  59.  
  60.    BytesTmp = (XByte And &H1) * &H80
  61.    BytesTmp = BytesTmp Or (XByte And &H2) * &H20
  62.    BytesTmp = BytesTmp Or (XByte And &H4) * &H8
  63.    BytesTmp = BytesTmp Or (XByte And &H8) * &H2
  64.  
  65.    BytesTmp = BytesTmp Or (XByte And &H10) / &H2
  66.    BytesTmp = BytesTmp Or (XByte And &H20) / &H8
  67.    BytesTmp = BytesTmp Or (XByte And &H40) / &H20
  68.    BytesTmp = BytesTmp Or (XByte And &H80) / &H80
  69.  
  70.    RotBits = BytesTmp
  71. End Function
  72.  
  73. ' Makers...
  74.  
  75. ' HWord & LWord = 8 Bytes
  76. Function MakeDWord(HWord As Integer, LWord As Integer) As Long
  77.    MakeDWord = (HWord * &H10000) Or (LWord And &HFFFF&)
  78. End Function
  79.  
  80. ' hByte & Lbyte = 4 bytes
  81. Function MakeWord(hByte As Byte, LByte As Byte) As Integer
  82.    If hByte And &H80 Then
  83.        MakeWord = ((hByte * &H100&) Or LByte) Or &HFFFF0000
  84.    Else
  85.        MakeWord = (hByte * &H100) Or LByte
  86.    End If
  87. End Function
  88.  
  89. ' By Raul100 @ Raul_hernandez_u@hotmail.com
  90. ' no quitar este texto
  91.  
  92.  



Código
  1. HLDWORD ( 1410872291 )

Código:
INPUT:
1410872291
0101 0100 0001 1000 0011 0011 1110 0011

OUTPUT
706268359
0010 1010 0001 1000 1100 1100 1100 0111






Título: Re: [Sources Code] RotBits [Byte TO Byte ]
Publicado por: BlackZeroX en 21 Octubre 2011, 06:30 am
.
El algoritmo esta bueno para hacer una encriptacion de longitud 32bits.

Dulces Lunas!¡.


Título: Re: [Sources Code] RotBits [Byte TO Byte ]
Publicado por: BlackZeroX en 21 Octubre 2011, 07:00 am
mira que casualidad recuerdas cuantas lineas de codigo se escribieron anteriormente para invertir bits adecuadamente y no por byte a byte... es decir los 4 bytes de un jalon con las funciones que has dejado se hace mas corto.

Código
  1.  
  2. Public Function Rot32Bits(ByVal lLong As Long) As Long
  3. '   //  Rota los bits de un Long (DWord)
  4. '   //  Manda el 1er bit al final el segundo al antepenultimo, el tercero uno antes del antepenultimo, etc...
  5. '   //  (1410872291) -> 0101 0100 0001 1000 0011 0011 1110 0011
  6. '   //  Invertidos:
  7. '   //  (-942925782) -> 1100 0111 1100 1100 0001 1000 0010 1010
  8. Dim iLWord      As Integer
  9. Dim iHWord      As Integer
  10.  
  11.    iLWord = LWord(lLong)
  12.    iHWord = HWord(lLong)
  13.    If (lLong And &H1) Then
  14.        Rot32Bits = ((RotBits(LByte(iLWord)) And &H7F) * &H1000000) Or &H80000000
  15.    Else
  16.        Rot32Bits = (RotBits(LByte(iLWord)) * &H1000000)
  17.    End If
  18.    Rot32Bits = Rot32Bits Or _
  19.               (RotBits(hByte(iLWord)) * &H10000) Or _
  20.               (RotBits(LByte(iHWord)) * &H100) Or _
  21.               (RotBits(hByte(iHWord)))
  22. End Function
  23.  
  24.  

Dulces Lunas!¡.


Título: Re: [Sources Code] RotBits [Byte TO Byte ]
Publicado por: x64core en 21 Octubre 2011, 07:09 am
.
El algoritmo esta bueno para hacer una encriptacion de longitud 32bits.

Dulces Lunas!¡.

si, eso estaba pensando cuando estaba escribiendola  :D

mira que casualidad recuerdas cuantas lineas de codigo se escribieron anteriormente para invertir bits adecuadamente y no por byte a byte... es decir los 4 bytes de un jalon con las funciones que has dejado se hace mas corto.

Código
  1.  
  2. Public Function Rot4Bits(ByVal lLong As Long) As Long
  3. '   //  Rota los bits de un Long (DWord)
  4. '   //  Manda el 1er bit al final el segundo al antepenultimo, el tercero uno antes del antepenultimo, etc...
  5. '   //  (1410872291) -> 0101 0100 0001 1000 0011 0011 1110 0011
  6. '   //  Invertidos:
  7. '   //  (-942925782) -> 1100 0111 1100 1100 0001 1000 0010 1010
  8. Dim iLWord      As Integer
  9. Dim iHWord      As Integer
  10.  
  11.    iLWord = LWord(lLong)
  12.    iHWord = HWord(lLong)
  13.    If (lLong And &H1) Then
  14.        Rot4Bits = ((RotBits(LByte(iLWord)) And &H7F) * &H1000000) Or &H80000000
  15.    Else
  16.        Rot4Bits = (RotBits(LByte(iLWord)) * &H1000000)
  17.    End If
  18.    Rot4Bits = Rot4Bits Or _
  19.               (RotBits(hByte(iLWord)) * &H10000) Or _
  20.               (RotBits(LByte(iHWord)) * &H100) Or _
  21.               (RotBits(hByte(iHWord)))
  22. End Function
  23.  
  24.  

Dulces Lunas!¡.

andale  >:D esta bueno!
pero si inserto un valor tipo byte o integer siempre me devuelve un long :P


Título: Re: [Sources Code] RotBits [Byte TO Byte ]
Publicado por: BlackZeroX en 21 Octubre 2011, 09:01 am
pero si inserto un valor tipo byte o integer siempre me devuelve un long :P

Código
  1.  
  2. Option Explicit
  3.  
  4. Private Declare Sub PutMem4 Lib "msvbvm60" (ByVal Addr As Long, ByVal NewVal As Long)
  5. Private Declare Sub GetMem4 Lib "msvbvm60" (ByVal Addr As Long, ByVal RetVal As Long)
  6.  
  7. ' DWORD = LONG... ?
  8. ' HIGH/LOW DWORD
  9. Function hlDWORD(ByVal DWord As Long) As Long
  10.    hlDWORD = MakeDWord(hlWORD(hWord(DWord)), hlWORD(lWord(DWord)))
  11. End Function
  12.  
  13. ' HIGH/LOW WORD
  14. Function hlWORD(ByVal Word As Integer) As Integer
  15.    hlWORD = MakeWord(Rot8Bits(hByte(Word)), Rot8Bits(lByte(Word)))
  16. End Function
  17.  
  18. Function hWord(ByVal DWord As Long) As Integer
  19.    hWord = (DWord \ &H10000)
  20. End Function
  21.  
  22. Function lWord(ByVal DWord As Long) As Integer
  23.    lWord = DWord And &HFFFF&
  24. End Function
  25.  
  26. Function hByte(ByVal iWord As Integer) As Byte
  27.    hByte = (iWord \ &H100) And &HFF
  28. End Function
  29.  
  30. Function lByte(ByVal Word As Integer) As Byte
  31.  lByte = (Word And &HFF)
  32. End Function
  33.  
  34. ' Makers...
  35.  
  36. ' HWord & LWord = 8 bits
  37. Function MakeDWord(ByVal hWord As Integer, ByVal lWord As Integer) As Long
  38.    MakeDWord = (hWord * &H10000) Or (lWord And &HFFFF&)
  39. End Function
  40.  
  41. ' hByte & Lbyte = 4 bits
  42. Function MakeWord(ByVal hByte As Byte, ByVal lByte As Byte) As Integer
  43.    MakeWord = (((hByte And &H7F) * &H100&) Or lByte)
  44.    If hByte And &H80 Then MakeWord = MakeWord Or &H8000
  45. End Function
  46.  
  47. ' 1 byte, Rot8Bits
  48. Function Rot8Bits(ByVal xByte As Byte) As Byte
  49. Dim BytesTmp    As Byte
  50.  
  51.    BytesTmp = (xByte And &H1) * &H80
  52.    BytesTmp = BytesTmp Or (xByte And &H2) * &H20
  53.    BytesTmp = BytesTmp Or (xByte And &H4) * &H8
  54.    BytesTmp = BytesTmp Or (xByte And &H8) * &H2
  55.  
  56.    BytesTmp = BytesTmp Or (xByte And &H10) / &H2
  57.    BytesTmp = BytesTmp Or (xByte And &H20) / &H8
  58.    BytesTmp = BytesTmp Or (xByte And &H40) / &H20
  59.    BytesTmp = BytesTmp Or (xByte And &H80) / &H80
  60.  
  61.    Rot8Bits = BytesTmp
  62. End Function
  63.  
  64. ' By Raul100 @ Raul_hernandez_u@hotmail.com
  65. ' no quitar este texto
  66.  
  67. Public Function Rot16Bits(ByVal iWord As Integer) As Integer
  68. '   //  Rota los bits de un integer (DWord)
  69.    Rot16Bits = ((Rot8Bits(lByte(iWord)) And &H7F) * &H100)
  70.    If (iWord And &H1) Then Rot16Bits = Rot16Bits Or &H8000
  71.    Rot16Bits = Rot16Bits Or (Rot8Bits(hByte(iWord)))
  72. End Function
  73.  
  74. Public Function Rot32Bits(ByVal lLong As Long) As Long
  75. '   //  Rota los bits de un Long (DWord)
  76. '   //  Manda el 1er bit al final el segundo al antepenultimo, el tercero uno antes del antepenultimo, etc...
  77. '   //  (1410872291) -> 0101 0100 0001 1000 0011 0011 1110 0011
  78. '   //  Invertidos:
  79. '   //  (-942925782) -> 1100 0111 1100 1100 0001 1000 0010 1010
  80.    Rot32Bits = &H10000 * (Rot16Bits(lWord(lLong)) And &H7FFF)
  81.    Rot32Bits = Rot32Bits Or Rot16Bits(hWord(lLong))
  82.    If (lLong And &H1) Then Rot32Bits = Rot32Bits Or &H80000000
  83. End Function
  84.  
  85. Public Function Rot64Bits(ByVal lDouble As Double) As Double
  86. '   //  Rota los bits de un Double (QWord)
  87. '   //  Manda el 1er bit al final el segundo al antepenultimo, el tercero uno antes del antepenultimo, etc...
  88. Dim lDWord  As Long
  89.    GetMem4 ByVal VarPtr(lDouble), ByVal VarPtr(lDWord)
  90.    PutMem4 ByVal (VarPtr(Rot64Bits) + &H4), Rot32Bits(lDWord)
  91.    GetMem4 ByVal (VarPtr(lDouble) + &H4), ByVal VarPtr(lDWord)
  92.    PutMem4 ByVal VarPtr(Rot64Bits), Rot32Bits(lDWord)
  93. End Function
  94.  
  95.  

Código
  1.  
  2. Private Sub Form_Load()
  3. Const VALOR8BITS        As Byte = &H54          '   //  MAX &HFF
  4. Const VALOR16BITS       As Integer = &H4F3F     '   //  MAX &HFFFF
  5. Const VALOR32BITS       As Long = &H541833E3    '   //  MAX &HFFFFFFFF
  6. Const VALOR64BITS       As Double = 1.08086391056892E+18   '   //  MAX &HFFFFFFFFFFFFFFFF (En Hex solo llega a 32bits)
  7.    Debug.Print Rot8Bits(VALOR8BITS)
  8.    Debug.Print Rot16Bits(VALOR16BITS)
  9.    Debug.Print Rot32Bits(VALOR32BITS)
  10.    Debug.Print Rot64Bits(VALOR64BITS)
  11.    Debug.Print hlDWORD(VALOR32BITS)
  12. End Sub
  13.  
  14.  

P.D.: No me citen el post que se hace largo el hilo

Dulces Lunas!¡.


Título: Re: [Sources Code] RotBits [Byte TO Byte ]
Publicado por: x64core en 21 Octubre 2011, 20:25 pm
 :laugh: me impresionas BlackZeroX  >:D

yo mejore esta funcion :P espero no equivocarme :P

Código
  1. ' ==> Bits
  2. Function Hbyte(Word As Integer) As Byte
  3. Hbyte = (Word / &H100)
  4. End Function

e visto que mi funcion se puede mejorar pasando arrays como parametros, tambien hice una version con la RtlMoveMemory
y funciono pero es menos rapida que trabajar directamente con los bits :P


Título: Re: [Sources Code] RotBits [Byte TO Byte ]
Publicado por: BlackZeroX en 21 Octubre 2011, 20:38 pm
En efecto pero le falta una mascara... al dividir un numero negativo los bits de mayor peso se setean en 1...

Código
  1.  
  2. Function hByte(iWord As Integer) As Byte
  3.    hByte = (iWord / &H100) And &HFF
  4. End Function
  5.  
  6.  

P.D.: Optimice todas las funciones Aqui (http://foro.elhacker.net/programacion_visual_basic/sources_code_rotbits_byte_to_byte-t342467.0.html;msg1676295#msg1676295), y agregue una funcion para rotar 64bits.

Dulces Lunas!¡.


Título: Re: [Sources Code] RotBits [Byte TO Byte ]
Publicado por: CAR3S? en 22 Octubre 2011, 00:40 am
Una pequeña pregunta... ¿Para que sirve?  :rolleyes:


Título: Re: [Sources Code] RotBits [Byte TO Byte ]
Publicado por: BlackZeroX en 22 Octubre 2011, 00:46 am
Su implementacion es variada, por ejemplo Rot16Bits() seria un reemplazdo de htons@Ws2_32 (API) (http://msdn.microsoft.com/en-us/library/ms738557(VS.85).aspx).

leete esto...
https://secure.wikimedia.org/wikipedia/es/wiki/Bit
https://secure.wikimedia.org/wikipedia/es/wiki/Nibble
https://secure.wikimedia.org/wikipedia/es/wiki/Sistema_binario

Dulces Lunas!¡.