2011-09-10 44 views
10

cómo convertir cadena de hexágono en base64? Encontré esta página http://home2.paulschou.net/tools/xlate/ pero necesito alguna función en java: String base64 = ...decoder(String hex); Encontré algo en Internet pero son difíciles de usar y usan array de bytes. Estoy buscando algo simplier thx muchocómo convertir a hexadecimal a base64

+0

Es probable (si lo sabes o no) Sea convertir hexagonal de bytes, luego los bytes vuelven a base64. –

Respuesta

18

Para consultar todas Commons Codec:

byte[] decodedHex = Hex.decodeHex(hex); 
byte[] encodedHexB64 = Base64.codeBase64(decodedHex); 
1

No es probable que encuentre algo que se convierta directamente de hexadecimal a base-64. Tendrá que buscar o escribir un decodificador hexadecimal y un codificador de base 64, y usar un byte[] como una forma intermedia.

comparar la realidad con lo que están pidiendo para:

String b64 = encoder.encode(decoder.decode(hex)); /* Too difficult :(!!! */ 

frente

String b64 = transcoder.transcode(hex);   /* So much simpler! */ 
1

Es posible que desee echa un vistazo a este código. Lo encontré en Google

import java.io.UnsupportedEncodingException; 
import java.security.MessageDigest; 
import java.security.NoSuchAlgorithmException; 

import sun.misc.BASE64Encoder; 

public class MD5 { 

    static char[] carr = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; 

    public static String getBase64FromHEX(String input) { 

     byte barr[] = new byte[16]; 
     int bcnt = 0; 
     for (int i = 0; i < 32; i += 2) { 
      char c1 = input.charAt(i); 
      char c2 = input.charAt(i + 1); 
      int i1 = intFromChar(c1); 
      int i2 = intFromChar(c2); 

      barr[bcnt] = 0; 
      barr[bcnt] |= (byte) ((i1 & 0x0F) << 4); 
      barr[bcnt] |= (byte) (i2 & 0x0F); 
      bcnt++; 
     } 

     BASE64Encoder encoder = new BASE64Encoder(); 
     return encoder.encode(barr); 
    } 

    public static synchronized String getMD5_Base64(String input) { 
     // please note that we dont use digest, because if we 
     // cannot get digest, then the second time we have to call it 
     // again, which will fail again 
     MessageDigest digest = null; 

     try { 
      digest = MessageDigest.getInstance("MD5"); 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 

     if (digest == null) 
      return input; 

     // now everything is ok, go ahead 
     try { 
      digest.update(input.getBytes("UTF-8")); 
     } catch (java.io.UnsupportedEncodingException ex) { 
      ex.printStackTrace(); 
     } 
     byte[] rawData = digest.digest(); 
     BASE64Encoder bencoder = new BASE64Encoder(); 
     return bencoder.encode(rawData); 
    } 

    private static int intFromChar(char c) { 
     char clower = Character.toLowerCase(c); 
     for (int i = 0; i < carr.length; i++) { 
      if (clower == carr[i]) { 
       return i; 
      } 
     } 

     return 0; 
    } 

    public static void main(String[] args) { 

     //String password = args[0]; 
     String password = "test"; 

     MessageDigest digest = null; 

     try { 
      digest = MessageDigest.getInstance("MD5"); 
     } catch (NoSuchAlgorithmException e) { 
      e.printStackTrace(); 
     } 

     try { 
      digest.update(password.getBytes("UTF-8")); 
     } catch (UnsupportedEncodingException ex) { 
      ex.printStackTrace(); 
     } 

     byte[] rawData = digest.digest(); 
     StringBuffer printable = new StringBuffer(); 

     for (int i = 0; i < rawData.length; i++) { 
      printable.append(carr[((rawData[i] & 0xF0) >> 4)]); 
      printable.append(carr[(rawData[i] & 0x0F)]); 
     } 
     String phpbbPassword = printable.toString(); 

     System.out.println("PHPBB   : " + phpbbPassword); 
     System.out.println("MVNFORUM  : " + getMD5_Base64(password)); 
     System.out.println("PHPBB->MVNFORUM : " + getBase64FromHEX(phpbbPassword)); 
    } 

} 
0

Al principio hay que importar el Apache Commons Codec biblioteca! https://commons.apache.org/proper/commons-codec/archives/1.9/index.html

Aquí es la API de 1,9 versión http://commons.apache.org/proper/commons-codec/archives/1.9/apidocs/index.html

Entonces usted tiene que seguir estos 3 pasos

 //convert String to char array (1st step) 
     char[] charArray = myhexString.toCharArray(); 

     // decode the char array to byte[] (2nd step) 
     byte[] decodedHex = Hex.decodeHex(charArray); 

    // The String decoded to Base64 (3rd step) 
    String result= Base64.encodeBase64String(decodedHex); 
Cuestiones relacionadas