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

 

 


Tema destacado: Usando Git para manipular el directorio de trabajo, el índice y commits (segunda parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  Java
| | | |-+  [Base64 encoder/decoder]
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [Base64 encoder/decoder]  (Leído 2,190 veces)
xmbeat

Desconectado Desconectado

Mensajes: 7


Ver Perfil
[Base64 encoder/decoder]
« en: 13 Julio 2012, 05:55 am »

Un pequeno codigo que hice para codificar unas urls que me hacian falta
Código
  1. /*
  2. Copyright (c) <2012> <xmbeat::Juan Hebert Chable Covarrubias>
  3.  
  4. Permission is hereby granted, free of charge, to any
  5. person obtaining a copy of this software and associated
  6. documentation files (the "Software"), to deal in the
  7. Software without restriction, including without limitation
  8. the rights to use, copy, modify, merge, publish,
  9. distribute, sublicense, and/or sell copies of the
  10. Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12.  
  13. The above copyright notice and this permission notice
  14. shall be included in all copies or substantial portions of
  15. the Software.
  16.  
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
  18. KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  19. WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  21. OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  23. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  24. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  25.  */
  26. public class Base64 {
  27. private static String TABLE_64;
  28. private static byte INDEX_TABLE[] = new byte[256];
  29. private char _padChar = '=';
  30. static
  31. {
  32. TABLE_64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  33. for (int i = 0; i < 64; i++)
  34. {
  35. INDEX_TABLE[TABLE_64.charAt(i)] = (byte)i;
  36. }
  37. }
  38. public Base64(){}
  39. public Base64(char padChar)
  40. {
  41. _padChar = padChar;
  42. }
  43. public String encode(byte[] data)
  44. {
  45. int finalLen = data.length / 3 * 4;
  46. if (data.length % 3 > 0)finalLen+=4;
  47. StringBuffer string64 = new StringBuffer();
  48. string64.setLength(finalLen);
  49. int leftBits = 0;
  50. int rightBits = 0;
  51. int shift = 2;
  52. int index = 0;
  53. for (int i = 0; i < data.length; i++)
  54. {
  55. int curByte = data[i] & 0xFF;
  56. leftBits = (rightBits | (curByte >> shift));
  57. string64.setCharAt(index++, TABLE_64.charAt(leftBits));
  58. rightBits = (curByte << (6-shift)) & 0x3F;
  59. if (shift == 6 || i == data.length - 1)
  60. {
  61. string64.setCharAt(index++,TABLE_64.charAt(rightBits));
  62. rightBits = 0;
  63. shift = 2;
  64. }
  65. else
  66. shift+=2;
  67. }
  68. for (;index < finalLen; index++)
  69. {
  70. string64.setCharAt(index, _padChar);
  71. }
  72. return string64.toString();
  73. }
  74. public byte[] decode(String data)
  75. {
  76. int realLen  = data.length() - 1;
  77. while(data.charAt(realLen)==_padChar) realLen--;
  78. realLen++;
  79. byte []result =  new byte[realLen*3/4];
  80. int rightBits = 0;
  81. int leftBits = 0;
  82. int shift = 2;
  83. int index = 0;
  84. boolean procesado = false;
  85. for (int i = 0; i < realLen; i++)
  86. {
  87. int curByte = INDEX_TABLE[data.charAt(i)] & 0xFF;
  88. //Incluí todos los caractares a una tabla, para asi ganar un poco de velocidad
  89. //Pero a consecuencia de que si un byte no esta en la tabla lo trata como la letra
  90. //'A', si quieren pongan un condicional para generar una excepcion
  91. rightBits = curByte >> (8-shift);
  92. if (procesado)
  93. {
  94. result[index++]=(byte)(rightBits | leftBits);
  95. }
  96. leftBits = (curByte << shift) & 0xFF;
  97. if (shift == 8)
  98. {
  99. procesado = false;
  100. shift = 2;
  101. }
  102. else
  103. {
  104. procesado = true;
  105. shift += 2;
  106. }
  107. }
  108. return result;
  109. }
  110. public static void main(String[] args)
  111. {
  112. Base64 converter = new Base64();
  113. String strEncode = converter.encode("A la ***** la universidad, yo quiero ser actor porno".getBytes());
  114. System.out.println(strEncode);
  115. System.out.println(new String(converter.decode(strEncode)));
  116. }
  117. }
  118.  
  119.  


En línea

Debci
Wiki

Desconectado Desconectado

Mensajes: 2.021


Actualizate o muere!


Ver Perfil WWW
Re: [Base64 encoder/decoder]
« Respuesta #1 en: 13 Julio 2012, 22:08 pm »

Muchísimas gracias por tu aporte, esto nos será de mucha ayuda, sobretodo para comprender el método de conversión aplicado a un algoritmo.


Saludos y bienvenido al foro :P


En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

Mensajes similares
Asunto Iniciado por Respuestas Vistas Último mensaje
encoder anti avg
Hacking
fsnpower 1 2,779 Último mensaje 14 Febrero 2016, 19:32 pm
por abaadoon
[Delphi] Base64 Image Encoder 0.2
Programación General
BigBear 2 2,656 Último mensaje 18 Septiembre 2016, 18:13 pm
por BigBear
Text Converter Encoder Decoder Stylish Text 4.0.1 Apk Premium
Software
Dmn82 0 1,268 Último mensaje 1 Marzo 2019, 17:58 pm
por Dmn82
ASCII a Base64 (encoder - javascript)
Desarrollo Web
@XSStringManolo 5 2,364 Último mensaje 9 Agosto 2019, 07:23 am
por @XSStringManolo
Base64 a ASCII (decoder - javascript)
Desarrollo Web
@XSStringManolo 2 1,742 Último mensaje 10 Agosto 2019, 01:52 am
por @XSStringManolo
WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines