Foro de elhacker.net

Programación => Java => Mensaje iniciado por: xmbeat en 13 Julio 2012, 05:55 am



Título: [Base64 encoder/decoder]
Publicado por: xmbeat 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.  


Título: Re: [Base64 encoder/decoder]
Publicado por: Debci 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