elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Security Series.XSS. [Cross Site Scripting]


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP)
| | | |-+  Programación Visual Basic (Moderadores: LeandroA, seba123neo)
| | | | |-+  [Reto] UrlEncode y UrlDecode
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] 2 Ir Abajo Respuesta Imprimir
Autor Tema: [Reto] UrlEncode y UrlDecode  (Leído 8,733 veces)
LeandroA
Moderador
***
Desconectado Desconectado

Mensajes: 760


www.leandroascierto.com


Ver Perfil WWW
[Reto] UrlEncode y UrlDecode
« en: 18 Diciembre 2012, 04:47 am »

Hola, se me presento la necesidad de crear esas funciones y en la web encontré algunas pero no funcionan muy bien asi que me pareció interesante el reto, no es muy dificil (creo), pero es para ver quien las puede hacer funcionar mas rapido.

Código:
Public Function URLDecode(ByVal sURL As String, Optional ByVal SpacePlus As Boolean = True) As String
Public Function URLEncode(ByVal sURL As String, Optional ByVal SpacePlus As Boolean = True) As String
el segundo parametro es opcional para remplazar espacios por +

es practicamente como lo que hace esta web http://meyerweb.com/eric/tools/dencoder/



osea ingresamos

Citar
si usamos la funcion urlEncode deberia cambiar el acento
Citar
por lo visto esta pasado a utf8 y luego a hex
lo importante es que encode los parámetros no la url entera ya que sino dejaria de ser una url valida.

otro ejemplos
Citar

Citar

después iremos debatiendo que esta mal o que falta.


En línea

MCKSys Argentina
Moderador Global
***
Desconectado Desconectado

Mensajes: 5.465


Diviértete crackeando, que para eso estamos!


Ver Perfil
Re: [Reto] UrlEncode y UrlDecode
« Respuesta #1 en: 18 Diciembre 2012, 14:09 pm »

Encontré errores en el código, así que lo quito.

Cuando lo tenga, lo pongo...  :P


« Última modificación: 18 Diciembre 2012, 15:35 pm por MCKSys Argentina » En línea

MCKSys Argentina

"Si piensas que algo está bien sólo porque todo el mundo lo cree, no estás pensando."

Danyfirex


Desconectado Desconectado

Mensajes: 493


My Dear Mizuho


Ver Perfil
Re: [Reto] UrlEncode y UrlDecode
« Respuesta #2 en: 18 Diciembre 2012, 17:18 pm »

Aquí esta el Encode  ;D creo que es lo que se quiere, al rato traigo el Decode


Código
  1. Function URLEncode(url As String) As String
  2. Dim sp() As Byte
  3. Dim final As String
  4.  
  5. sp() = StrConv(url, vbFromUnicode)
  6.  
  7. For i = 0 To UBound(sp)
  8.  
  9. Select Case sp(i)
  10.  
  11.    Case 45, 46, 48 To 57, 65 To 90, 95, 97 To 122, 126
  12.         final = final & Chr(sp(i))
  13.  
  14.    Case 32
  15.        final = final & "+"
  16.  
  17.   Case Else
  18.           final = final & "%" & Hex(sp(i))
  19. End Select
  20.  
  21. Next
  22. URLEncode= final
  23. End Function


Edito:

aqui esta el Decode.

Código
  1. Function URLDecode(url As String) As String
  2. Dim spl() As String
  3. Dim final As String
  4. Dim str As String
  5. str = Replace(url, "+", " ")
  6. spl() = Split(str, "%")
  7. final = spl(0)
  8. For i = 1 To UBound(spl)
  9. final = final & Chr(CLng("&H" & Left(spl(i), 2))) & Mid(spl(i), 3)
  10. Next
  11. URLDecode = final
  12. End Function

saludos

PD: lo hice a base de uno que vi en Autoit
« Última modificación: 18 Diciembre 2012, 21:00 pm por Danyfirex » En línea

cobein


Desconectado Desconectado

Mensajes: 759



Ver Perfil WWW
Re: [Reto] UrlEncode y UrlDecode
« Respuesta #3 en: 19 Diciembre 2012, 02:45 am »

Leandro no podes usar InternetCanonicalizeUrl ?

http://msdn.microsoft.com/en-us/library/windows/desktop/aa384342(v=vs.85).aspx
En línea

http://www.advancevb.com.ar
Más Argentino que el morcipan
Aguante el Uvita tinto, Tigre, Ford y seba123neo
Karcrack es un capo.
LeandroA
Moderador
***
Desconectado Desconectado

Mensajes: 760


www.leandroascierto.com


Ver Perfil WWW
Re: [Reto] UrlEncode y UrlDecode
« Respuesta #4 en: 19 Diciembre 2012, 04:28 am »

bien, ya estoy algo confuso, cobein probé con InternetCanonicalizeUrl no se si pueda decir si funciona o no es algo que no me queda claro, el api trabaja igual que  UrlEscape pero no codifica los caracteres al igual que cuando los copio de la barra del navegador, de todas formas la url parce andar bien.

Código
  1. Option Explicit
  2. Private Declare Sub InternetCanonicalizeUrl Lib "wininet.dll" Alias "InternetCanonicalizeUrlA" (ByVal lpszUrl As String, ByVal lpszBuffer As String, ByRef lpdwBufferLength As Long, ByVal dwFlags As Long)
  3. Private Const INTERNET_MAX_URL_LENGTH As Long = 2048
  4. Private Const ICU_BROWSER_MODE As Long = &H2000000
  5. Private Const ICU_DECODE As Long = &H10000000
  6. Private Const ICU_ENCODE_PERCENT As Long = &H1000
  7. Private Const ICU_ENCODE_SPACES_ONLY As Long = &H4000000
  8. Private Const ICU_NO_ENCODE As Long = &H20000000
  9. Private Const ICU_ESCAPE As Long = &H80000000
  10. Private Const ICU_NO_META As Long = &H8000000
  11.  
  12.  
  13. Private Sub Form_Load()
  14.    Debug.Print UrlEncode("https://www.google.com.ar/search?q=canción animal")
  15.    'https://www.google.com.ar/search?q=casa%20duplex&num=10&hl=es&safe=off&biw=1680&bih=925&sa=X&ei=mS7RUIqvHYjW8gSA9oHABg&ved=0CBkQpwUoAw&source=lnt&tbs=cdr%3A1%2Ccd_min%3A5%2F12%2F2012%2Ccd_max%3A18%2F12%2F2012&tbm=isch
  16.    Debug.Print UrlEncode("https://www.google.com.ar/search?q=casa duplex&num=10&hl=es&safe=off&biw=1680&bih=925&sa=X&ei=mS7RUIqvHYjW8gSA9oHABg&ved=0CBkQpwUoAw&source=lnt&tbs=cdr:1,cd_min:5/12/2012,cd_max:18/12/2012&tbm=isch")
  17. End Sub
  18.  
  19. Private Function UrlEncode(sURL As String, Optional ByVal SpacePlus As Boolean) As String
  20.  
  21.    Dim sBuffer As String, lBufferLength As Long
  22.  
  23.    sBuffer = String$(INTERNET_MAX_URL_LENGTH, 0)
  24.    lBufferLength = INTERNET_MAX_URL_LENGTH
  25.    InternetCanonicalizeUrl sURL, sBuffer, lBufferLength, ICU_ENCODE_PERCENT Or (ICU_ENCODE_SPACES_ONLY * SpacePlus)
  26.    If lBufferLength > 0 Then UrlEncode = Left$(sBuffer, lBufferLength)
  27.  
  28. End Function
  29.  

@Danyfirex la función no va por mal camino pero al remplazar los "&" la url queda inservible.

En línea

MCKSys Argentina
Moderador Global
***
Desconectado Desconectado

Mensajes: 5.465


Diviértete crackeando, que para eso estamos!


Ver Perfil
Re: [Reto] UrlEncode y UrlDecode
« Respuesta #5 en: 19 Diciembre 2012, 04:30 am »

Dejo mi Encode. El decode lo hago cuando se pase la F1ACA...  ;D

Código
  1. Option Explicit
  2. Option Base 0
  3.  
  4. Enum eMaxWinInetValues
  5.  INTERNET_MAX_HOST_NAME_LENGTH = 256
  6.  INTERNET_MAX_USER_NAME_LENGTH = 128
  7.  INTERNET_MAX_PASSWORD_LENGTH = 128
  8.  INTERNET_MAX_PORT_NUMBER_LENGTH = 5          ' INTERNET_PORT is unsigned short
  9.  INTERNET_MAX_PORT_NUMBER_VALUE = 65535       ' maximum unsigned short value
  10.  INTERNET_MAX_PATH_LENGTH = 2048
  11.  INTERNET_MAX_SCHEME_LENGTH = 32              ' longest protocol name length
  12.  INTERNET_MAX_URL_LENGTH = INTERNET_MAX_SCHEME_LENGTH + 3 + INTERNET_MAX_PATH_LENGTH
  13. End Enum
  14.  
  15. Public Type URL_COMPONENTSA
  16.  dwStructSize As Long
  17.  lpszScheme As String
  18.  dwSchemeLength As Long
  19.  nScheme As INTERNET_SCHEME
  20.  lpszHostName As String
  21.  dwHostNameLength As Long
  22.  nPort As Integer
  23.  
  24.  lpszUsername As String
  25.  dwUserNameLength As Long
  26.  lpszPassword As String
  27.  dwPasswordLength As Long
  28.  
  29.  lpszUrlPath As String
  30.  dwUrlPathLength As Long
  31.  lpszExtraInfo As String
  32.  dwExtraInfoLength As Long
  33. End Type
  34.  
  35. Enum eCanonizeURL
  36.  ICU_ESCAPE = &H80000000       ' (un)escape URL characters
  37.  ICU_DECODE = &H10000000       ' Convert %XX escape sequences To characters
  38. End Enum
  39.  
  40. Enum INTERNET_SCHEME
  41.    INTERNET_SCHEME_PARTIAL = -2
  42.    INTERNET_SCHEME_UNKNOWN = -1
  43.    INTERNET_SCHEME_DEFAULT = 0
  44.    INTERNET_SCHEME_FTP
  45.    INTERNET_SCHEME_GOPHER
  46.    INTERNET_SCHEME_HTTP
  47.    INTERNET_SCHEME_HTTPS
  48.    INTERNET_SCHEME_FILE
  49.    INTERNET_SCHEME_NEWS
  50.    INTERNET_SCHEME_MAILTO
  51.    INTERNET_SCHEME_SOCKS
  52.    INTERNET_SCHEME_FIRST = INTERNET_SCHEME_FTP
  53.    INTERNET_SCHEME_LAST = INTERNET_SCHEME_SOCKS
  54. End Enum
  55.  
  56. Declare Function InternetCrackUrl Lib "WININET" Alias "InternetCrackUrlA" ( _
  57.    ByVal lpszUrl As String, _
  58.    ByVal dwUrlLength As Long, _
  59.    ByVal dwFlags As eCanonizeURL, _
  60.    lpUrlComponents As URL_COMPONENTSA) As Long
  61.  
  62. Declare Function PathIsURL Lib "shlwapi" Alias "PathIsURLA" ( _
  63.    ByVal pszPath As String) As Long
  64.  
  65. Public Const URIReserved = "!#%&'()*+,/:;=?@[]" 'Caracteres reservados
  66.  
  67. Public Function CrackURL(ByVal URL As String) As URL_COMPONENTSA
  68.  Dim c As URL_COMPONENTSA, Result As Long
  69.  
  70.  c.dwStructSize = 60
  71.  c.lpszScheme = Space(INTERNET_MAX_SCHEME_LENGTH)
  72.  c.dwSchemeLength = INTERNET_MAX_SCHEME_LENGTH
  73.  c.lpszHostName = Space(INTERNET_MAX_HOST_NAME_LENGTH)
  74.  c.dwHostNameLength = INTERNET_MAX_HOST_NAME_LENGTH
  75.  
  76.  c.lpszUsername = Space(INTERNET_MAX_USER_NAME_LENGTH)
  77.  c.dwUserNameLength = INTERNET_MAX_USER_NAME_LENGTH
  78.  c.lpszPassword = Space(INTERNET_MAX_USER_NAME_LENGTH)
  79.  c.dwPasswordLength = INTERNET_MAX_USER_NAME_LENGTH
  80.  
  81.  c.lpszUrlPath = Space(INTERNET_MAX_PATH_LENGTH)
  82.  c.dwUrlPathLength = INTERNET_MAX_PATH_LENGTH
  83.  'fix: extend size of extraInfo to len of url
  84.  c.lpszExtraInfo = Space(Len(URL))
  85.  c.dwExtraInfoLength = Len(URL)
  86.  Result = InternetCrackUrl(URL, 0, 0, c)
  87.  If Result Then
  88.    c.lpszScheme = Left$(c.lpszScheme, c.dwSchemeLength)
  89.    c.lpszHostName = Left$(c.lpszHostName, c.dwHostNameLength)
  90.  
  91.    c.lpszUsername = Left$(c.lpszUsername, c.dwUserNameLength)
  92.    c.lpszPassword = Left$(c.lpszPassword, c.dwPasswordLength)
  93.  
  94.    c.lpszUrlPath = Left$(c.lpszUrlPath, c.dwUrlPathLength)
  95.    c.lpszExtraInfo = Left$(c.lpszExtraInfo, c.dwExtraInfoLength)
  96.  Else
  97.    c.lpszScheme = ""
  98.    c.lpszHostName = ""
  99.  
  100.    c.lpszUsername = ""
  101.    c.lpszPassword = ""
  102.  
  103.    c.lpszUrlPath = ""
  104.    c.lpszExtraInfo = ""
  105.  End If
  106.  CrackURL = c
  107. End Function
  108.  
  109. Function isPrint(Car As String) As Boolean
  110. 'Devuelve si un caracter es imprimible (según: http://en.wikipedia.org/wiki/Isprint)
  111. 'Similar a isprint de C
  112. If Len(Car) <> 1 Then
  113.    isPrint = False
  114. Else
  115.    isPrint = (Asc(Car) >= 32) And (Asc(Car) <= 126)
  116. End If
  117. End Function
  118.  
  119. Function HexB(Num As Byte) As String
  120. 'Devuel el valor Hexa de un byte con padding
  121. Dim strAux As String
  122.  
  123. strAux = Hex(Num)
  124. If Len(strAux) = 1 Then
  125.    strAux = "0" + strAux
  126. End If
  127. HexB = strAux
  128. End Function
  129.  
  130. Public Function EncodeAscii(Cadena As String, SpacePlus As Boolean) As String
  131. Dim I As Long
  132. Dim Car As String
  133. Dim sRet As String
  134.  
  135. sRet = ""
  136. For I = 1 To Len(Cadena)
  137.    Car = Mid(Cadena, I, 1)
  138.    If (InStr(1, URIReserved, Car) > 0) Or (Not isPrint(Car)) Then
  139.        'contempla los espacios
  140.        If SpacePlus And (Asc(Car) = 32) Then
  141.            sRet = sRet + "+"
  142.        Else
  143.            sRet = sRet + "%"
  144.            sRet = sRet + HexB(Asc(Car))
  145.        End If
  146.    Else
  147.        'char comun
  148.        sRet = sRet + Car
  149.    End If
  150. Next I
  151. EncodeAscii = sRet
  152. End Function
  153.  
  154. Public Function isValidExtraInfo(Cadena As String) As Boolean
  155. isValidExtraInfo = (InStr(1, Cadena, "?") <> 0) And (InStr(1, Cadena, "=") <> 0)
  156. End Function
  157.  
  158. Public Function URLEncode(ByVal sURL As String, Optional ByVal SpacePlus As Boolean = True) As String
  159. Dim URLComponents As URL_COMPONENTSA
  160. Dim sParams As String
  161. Dim mExtraValues() As String
  162. Dim mExtraValuePair() As String
  163. Dim sRet As String
  164. Dim I As Long
  165. Dim Car As String
  166.  
  167. 'verifica que sea un URL válido
  168. If Not PathIsURL(sURL) Then URLEncode = ""
  169.  
  170. 'parsea URL
  171. URLComponents = CrackURL(sURL)
  172. 'URI = <scheme>://<host>/<path>;<params>?<query>#<fragment>
  173. 'M$ URL = <scheme>://<user>:<pass>@<host>/<path>;<params>?<query>#<fragment>
  174.  
  175. 'Re-Armar URL
  176. 'scheme
  177. If URLComponents.lpszScheme <> "" Then
  178.    sRet = URLComponents.lpszScheme + "://"
  179. End If
  180. 'user,pass,host,port
  181. If URLComponents.lpszUsername <> "" Then
  182.    sRet = sRet + URLComponents.lpszUsername + ":"
  183. End If
  184. If URLComponents.lpszPassword <> "" Then
  185.    sRet = sRet + URLComponents.lpszPassword + "@"
  186. End If
  187. sRet = sRet + URLComponents.lpszHostName
  188. If URLComponents.nPort <> 0 Then
  189.    sRet = sRet + ":" & URLComponents.nPort
  190. End If
  191. 'path
  192. sRet = sRet + URLComponents.lpszUrlPath
  193.  
  194. 'Ahora lo pedido: convertir los extras
  195. 'extras format: "?" + <ID> + "=" + <Value> + "&"
  196. 'lo que se encodea es sólo <Value>
  197. If (URLComponents.dwExtraInfoLength > 4) And isValidExtraInfo(URLComponents.lpszExtraInfo) Then
  198.    'para evitar errores, el largo MINIMO deberia ser de 4 caracteres: "?" + <ID> + "=" + <Value>
  199.    sRet = sRet + "?"
  200.    sParams = Mid(URLComponents.lpszExtraInfo, 2, Len(URLComponents.lpszExtraInfo))
  201.    mExtraValues = Split(sParams, "&")
  202.    For I = 0 To UBound(mExtraValues)
  203.        mExtraValuePair = Split(mExtraValues(I), "=")
  204.        sRet = sRet + mExtraValuePair(0) + "=" + EncodeAscii(mExtraValuePair(1), SpacePlus) + "&"
  205.    Next I
  206.    'borrar '&' final
  207.    sRet = Mid(sRet, 1, Len(sRet) - 1)
  208. ElseIf URLComponents.dwExtraInfoLength > 0 Then
  209.    sRet = sRet + URLComponents.lpszExtraInfo
  210. End If
  211. URLEncode = sRet
  212. End Function
  213.  
  214. Public Function URLDecode(ByVal sURL As String, Optional ByVal SpacePlus As Boolean = True) As String
  215. ''Lo inverso al Encode :)
  216. End Function
  217.  

EDIT: El código para parsear el URL lo saqué de acá: http://www.motobit.com/tips/detpg_CrackURL/

EDIT 2: Una cosa que noto ahora es que si la parte de los querys tiene "&" ó "=" demás, el parseo va a fallar...  :-\
« Última modificación: 19 Diciembre 2012, 04:55 am por MCKSys Argentina » En línea

MCKSys Argentina

"Si piensas que algo está bien sólo porque todo el mundo lo cree, no estás pensando."

LeandroA
Moderador
***
Desconectado Desconectado

Mensajes: 760


www.leandroascierto.com


Ver Perfil WWW
Re: [Reto] UrlEncode y UrlDecode
« Respuesta #6 en: 19 Diciembre 2012, 07:27 am »

Tiro una utilizando las funciones de javascript, haciendo unos malabares para preservar una url valida

Código
  1. Public Function URLEncode(ByVal sUrl As String) As String
  2.    Dim objSC As Object
  3.    Dim sPart() As String
  4.    sPart = Split(sUrl, "?")
  5.    If UBound(sPart) > 0 Then
  6.        Set objSC = CreateObject("ScriptControl")
  7.        objSC.Language = "Jscript"
  8.        sPart(1) = objSC.CodeObject.encodeURIComponent(sPart(1))
  9.        sPart(1) = Replace(sPart(1), "%3D", "=")
  10.        sPart(1) = Replace(sPart(1), "%26", "&")
  11.        Set objSC = Nothing
  12.    End If
  13.    URLEncode = Join(sPart, "?")
  14. End Function
  15.  
  16. Public Function URLDecode(ByVal sUrl As String) As String
  17.    Dim objSC As Object
  18.    Set objSC = CreateObject("ScriptControl")
  19.    objSC.Language = "Jscript"
  20.    URLDecode = objSC.CodeObject.decodeURIComponent(sUrl)
  21.    Set objSC = Nothing
  22. End Function
  23.  

PD utiliza coficiación utf8
alguien sabe si CreateObject("ScriptControl") es valido para una pc que no tenga instaldo el vb?, o es una libreria que trae  windows
« Última modificación: 19 Diciembre 2012, 07:31 am por LeandroA » En línea

cobein


Desconectado Desconectado

Mensajes: 759



Ver Perfil WWW
Re: [Reto] UrlEncode y UrlDecode
« Respuesta #7 en: 19 Diciembre 2012, 14:27 pm »

Leandro aca arme un funcion que replica lo que hace la web que posteaste. Las funciones para UTF8 las saque de tu code.

Hay que limpiar ese code porque quedo medio desastre pero bueno es simplemente a modo de ejemplo.

Nota: los caracteres reservados los saque de http://en.wikipedia.org/wiki/Percent-encoding

Código:
Option Explicit

Private Declare Function WideCharToMultiByte Lib "KERNEL32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long
Private Declare Function MultiByteToWideChar Lib "KERNEL32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
Private Const CP_UTF8                           As Long = 65001

Private Sub Form_Load()
    Debug.Print (EncodeURL("http://www.taringa.net/buscar/?q=día 12/12/12&interval="))
End Sub

Private Function EncodeURL(ByVal sURL As String) As String
    Dim bvData()    As Byte
    Dim i           As Long
    Dim sChar       As String * 1
    
    bvData = Unicode2UTF8(sURL)
    
    For i = 0 To UBound(bvData) Step 2
        sChar = Chr$(bvData(i))
        Select Case sChar
            Case "a" To "z", "A" To "Z", "0" To "9", "-", "_", ".", "~"
                EncodeURL = EncodeURL & sChar
            Case Else
                EncodeURL = EncodeURL & "%" & Right$("0" & Hex(Asc(sChar)), 2)
        End Select
    Next
End Function

Private Function DecodeURL(ByVal sURL As String) As String
    Dim bvData()    As Byte
    Dim i           As Long
    Dim sChar       As String * 1
    bvData = sURL
        
    For i = 0 To UBound(bvData) Step 2
        sChar = Chr$(bvData(i))
        If sChar = "%" Then
            DecodeURL = DecodeURL & Chr$(Val("&h" & Chr$(bvData(i + 2)) & Chr$(bvData(i + 4))))
            i = i + 4
        Else
        DecodeURL = DecodeURL & sChar
        End If
    Next
    DecodeURL = UTF82Unicode(DecodeURL)
End Function

Private Function UTF82Unicode(ByVal sUTF8 As String) As String

    Dim UTF8Size As Long
    Dim BufferSize As Long
    Dim BufferUNI As String
    Dim LenUNI As Long
    Dim bUTF8() As Byte
    
    If LenB(sUTF8) = 0 Then Exit Function
    
    bUTF8 = StrConv(sUTF8, vbFromUnicode)
    UTF8Size = UBound(bUTF8) + 1
    
    BufferSize = UTF8Size * 2
    BufferUNI = String$(BufferSize, vbNullChar)
    
    LenUNI = MultiByteToWideChar(CP_UTF8, 0, bUTF8(0), UTF8Size, StrPtr(BufferUNI), BufferSize)
    
    If LenUNI Then
        UTF82Unicode = Left$(BufferUNI, LenUNI)
    End If

End Function


Private Function Unicode2UTF8(ByVal strUnicode As String) As String

    Dim LenUNI As Long
    Dim BufferSize As Long
    Dim LenUTF8 As Long
    Dim bUTF8() As Byte
    
    LenUNI = Len(strUnicode)
    
    If LenUNI = 0 Then Exit Function
    
    BufferSize = LenUNI * 3 + 1
    ReDim bUTF8(BufferSize - 1)
    
    LenUTF8 = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strUnicode), LenUNI, bUTF8(0), BufferSize, vbNullString, 0)
    
    If LenUTF8 Then
        ReDim Preserve bUTF8(LenUTF8 - 1)
        Unicode2UTF8 = StrConv(bUTF8, vbUnicode)
    End If

End Function
« Última modificación: 19 Diciembre 2012, 14:29 pm por cobein » En línea

http://www.advancevb.com.ar
Más Argentino que el morcipan
Aguante el Uvita tinto, Tigre, Ford y seba123neo
Karcrack es un capo.
Danyfirex


Desconectado Desconectado

Mensajes: 493


My Dear Mizuho


Ver Perfil
Re: [Reto] UrlEncode y UrlDecode
« Respuesta #8 en: 19 Diciembre 2012, 15:54 pm »


@Danyfirex la función no va por mal camino pero al remplazar los "&" la url queda inservible.



No entiendo eso Que me dices? :S

igual aquí dejo un poco optimizada la función agregando Las funciones para UTF8.

Y ahora si simula bien como la pagina. http://meyerweb.com/eric/tools/dencoder/

Código
  1. Private Declare Function WideCharToMultiByte Lib "KERNEL32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpDefaultChar As String, ByVal lpUsedDefaultChar As Long) As Long
  2. Private Declare Function MultiByteToWideChar Lib "KERNEL32" (ByVal CodePage As Long, ByVal dwFlags As Long, ByRef lpMultiByteStr As Any, ByVal cchMultiByte As Long, ByVal lpWideCharStr As Long, ByVal cchWideChar As Long) As Long
  3. Private Const CP_UTF8                           As Long = 65001
  4.  
  5.  
  6. Private Sub Form_Load()
  7. Debug.Print (URLEncode("https://www.google.com.ar/search?q=canción"))
  8. Debug.Print URLDecode(URLEncode("https://www.google.com.ar/search?q=canción"))
  9. End Sub
  10.  
  11.  
  12. Function URLEncode(url As String) As String
  13. Dim sp() As Byte
  14. Dim final As String
  15.  
  16. sp() = StrConv(Unicode2UTF8(url), vbFromUnicode)
  17.  
  18. For i = 0 To UBound(sp)
  19.  
  20. Select Case sp(i)
  21.  
  22.    Case 45, 46, 48 To 57, 65 To 90, 95, 97 To 122, 126
  23.         final = final & Chr(sp(i))
  24.  
  25.    Case 32
  26.        final = final & "+"
  27.  
  28.   Case Else
  29.           final = final & "%" & Hex(sp(i))
  30. End Select
  31.  
  32. Next
  33. URLEncode = final
  34. End Function
  35.  
  36. Function URLDecode(url As String) As String
  37. Dim spl() As String
  38. Dim final As String
  39. Dim str As String
  40. str = Replace(url, "+", " ")
  41. spl() = Split(str, "%")
  42. final = spl(0)
  43. For i = 1 To UBound(spl)
  44. final = final & Chr(CLng("&H" & Left(spl(i), 2))) & Mid(spl(i), 3)
  45. Next
  46. URLDecode = UTF82Unicode(final)
  47. End Function
  48.  
  49.  
  50.  
  51. Private Function UTF82Unicode(ByVal sUTF8 As String) As String
  52.  
  53.    Dim UTF8Size As Long
  54.    Dim BufferSize As Long
  55.    Dim BufferUNI As String
  56.    Dim LenUNI As Long
  57.    Dim bUTF8() As Byte
  58.  
  59.    If LenB(sUTF8) = 0 Then Exit Function
  60.  
  61.    bUTF8 = StrConv(sUTF8, vbFromUnicode)
  62.    UTF8Size = UBound(bUTF8) + 1
  63.  
  64.    BufferSize = UTF8Size * 2
  65.    BufferUNI = String$(BufferSize, vbNullChar)
  66.  
  67.    LenUNI = MultiByteToWideChar(CP_UTF8, 0, bUTF8(0), UTF8Size, StrPtr(BufferUNI), BufferSize)
  68.  
  69.    If LenUNI Then
  70.        UTF82Unicode = Left$(BufferUNI, LenUNI)
  71.    End If
  72.  
  73. End Function
  74.  
  75.  
  76. Private Function Unicode2UTF8(ByVal strUnicode As String) As String
  77.  
  78.    Dim LenUNI As Long
  79.    Dim BufferSize As Long
  80.    Dim LenUTF8 As Long
  81.    Dim bUTF8() As Byte
  82.  
  83.    LenUNI = Len(strUnicode)
  84.  
  85.    If LenUNI = 0 Then Exit Function
  86.  
  87.    BufferSize = LenUNI * 3 + 1
  88.    ReDim bUTF8(BufferSize - 1)
  89.  
  90.    LenUTF8 = WideCharToMultiByte(CP_UTF8, 0, StrPtr(strUnicode), LenUNI, bUTF8(0), BufferSize, vbNullString, 0)
  91.  
  92.    If LenUTF8 Then
  93.        ReDim Preserve bUTF8(LenUTF8 - 1)
  94.        Unicode2UTF8 = StrConv(bUTF8, vbUnicode)
  95.    End If
  96.  
  97. End Function
  98.  

saludos
En línea

79137913


Desconectado Desconectado

Mensajes: 1.169


4 Esquinas


Ver Perfil WWW
Re: [Reto] UrlEncode y UrlDecode
« Respuesta #9 en: 20 Diciembre 2012, 12:52 pm »

HOLA!!!

Como va el reto, asi lo publico en la recopilación.

GRACIAS POR LEER!!!
En línea

"Como no se puede igualar a Dios, ya he decidido que hacer, ¡SUPERARLO!"
"La peor de las ignorancias es no saber corregirlas"

 79137913                          *Shadow Scouts Team*
Páginas: [1] 2 Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
Reto ;)
Ingeniería Inversa
NeoKiller 3 2,868 Último mensaje 15 Agosto 2004, 23:12 pm
por NeoKiller
Reto!!
Ingeniería Inversa
HaCkZaTaN 2 2,930 Último mensaje 10 Septiembre 2004, 09:30 am
por Ðevastador
eval(gzinflate(base64_decode(urldecode
PHP
luiggy2 8 8,731 Último mensaje 16 Junio 2008, 23:30 pm
por WHK
Alguien me puede ayudar con la funcion URLENCODE?
PHP
TrashAmbishion 1 3,753 Último mensaje 24 Agosto 2011, 22:54 pm
por WHK
[Pregunta]: urlencode se usa para codificar toda una url o una parte?
Desarrollo Web
Leguim 1 1,893 Último mensaje 16 Enero 2020, 18:58 pm
por engel lex
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines