Martes, 12 de abril de 2005
Base 64
Archivado en:
FIB
Algoritmo Base 64
Hace poco tuve que programar en Java una clase que implementara el algoritmo de conversión de una cadena de texto plano a otra en base64. Este formato es muy utilizado para la trasmisión de archivos a través de la red y asegura que no aparezcan caracteres extraños entre diferentes plataformas. He aqui la clase que programé (los comentarios están en catalán):
/*
* base64.java
*
* Created on 20 de febrero de 2005, 21:41
* Ended on 07 de marzo de 2005, 19:00
*/
/**
*
* @author Brian Jimenez Garcia
* @contact neofromatrix@gmail.com
* @assig Cryptography - FIB (www.fib.upc.es)
*/
public class base64
{
//Taula que conte la conversio dels caracters a base64.
private static char[] taula =
{'A','B','C','D','E','F','G','H',
'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X',
'Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3',
'4','5','6','7','8','9','+','/'};
public static String bitToBase64(byte[] b)
{
//String criptograma = "";
int longitud64 = (b.length * 8)/6;
StringBuffer criptograma = new StringBuffer();
byte temp = 0x00;
int i = 0;
int j = 0;
for(i=0;i
{
switch(i%4)
{
case 0:
temp = (byte)((b[i-j] >> 2) & (byte)0x3f);
break;
case 1:
temp = (byte)( ((byte)(b[i-j-1] << 4)) & 0x3f | ((byte)(b[i-j] >> 4) & (byte)0x0f) );
break;
case 2:
temp = (byte)( ((byte)(b[i-j-1] << 2))&(byte)0x3f | ((byte)(b[i-j] >> 6) & (byte)0x03) );
break;
case 3:
temp = (byte)(b[i-j-1] & (byte)0x3f);
j++;
break;
}
criptograma.append(taula[temp]);
}
i--;
if(b.length % 3 == 2)
{
temp = (byte)((byte)(b[i-j]<<2) & (byte)0x3f);
criptograma.append(taula[temp]+"=");
}
else if(b.length % 3 == 1){
temp = (byte)((byte)(b[i-j]<<4) & (byte)0x3f);
criptograma.append(taula[temp]+"==");
}
return criptograma.toString();
}
public static byte[] base64ToBit(String textb)
{
//Calcul de la longitud de la cadena de bytes
int longitud = textb.indexOf('=');
if(longitud == -1) longitud = (textb.length()*6)/8;
else longitud = (longitud*6 + (textb.length()-1-longitud)*2)/8;
byte[] b = new byte[longitud];
char[] c = textb.toCharArray();
int j = 0;
byte temp = 0x00;
for(int i=0;i
{
switch(i%3)
{
case 0:
b[i] = (byte)((obtenirByte(c[i+j]) << 2) | ((obtenirByte(c[i+1+j]) >> 4) & 0x03) );
break;
case 1:
b[i] = (byte)((obtenirByte(c[i+j])&0x0f)<<4 | obtenirByte(c[i+1+j])>>2 & 0x3f);
break;
case 2:
b[i] = (byte)((obtenirByte(c[i+j])<<6 ) | obtenirByte(c[i+j+1]));
j++;
break;
}
}
return b;
}
/*
* Funcio que retorna el valor numeric d'un caracter ACSII
* en base 64
*/
private static byte obtenirByte(char c)
{
int valor = (int)c;
if(valor == 47) return (byte)63;
if(valor == 43) return (byte)62;
if(valor>= 48 && valor <= 57)return (byte)(valor+4);
if(valor>= 65 && valor <= 90)return (byte)(valor-65);
if(valor>= 97 && valor <= 122)return (byte)(valor-71);
else return (byte)0; //Caracter '='
}
}
Más información sobre Base-64:
RFC-3548
Escrito por
Brian Jiménez El 04/12 a las 22:10
(2)
Comentarios •
(0)
Referencias •
Permalink
Referencias
URL para referencias
Comentarios
aixó esta malament,no se d'on ho has tret pero la primera funció no va bé,no se on has après tu a programar.. pero.. bueno.. aprèn millor.
Comentario de fitiplay el el 04/13 a las 20:44
Em dius que funciona malament...i quin argument aportes?
Comentario de Nakashima el el 04/14 a las 07:54
Comentar