2011-01-17 27 views
9

org.apache.commons.codec.binary.Base64 kullanıyorum utf8 dizesini deşifre ediyorum. Bazen, decode sonra örneğin ^@k��@@ gibi görünüyor base64 kodlanmış dize alır. Base64'ün doğru olup olmadığını veya utf8 stringinin utf8 stringinin geçerli olup olmadığını nasıl kontrol edebilirim? Açıklık getirmek içinJava BASE64 utf8 dize kod çözme

. Ben tam tersi byte[] ve String dönüştürme sırasında charset belirtmelidir

public static String base64Decode(String str) { 
    try { 
     return new String(base64Decode(str.getBytes(Constants.UTF_8)), Constants.UTF_8); 
    } catch (UnsupportedEncodingException e) { 
     ... 
    } 
} 

public static byte[] base64Decode(byte[] byteArray) { 
    return Base64.decodeBase64(byteArray); 
} 
+0

Dize "UTF-8" derken ne demek istiyorsunuz? Bir String nesnesi, kodlamalar ve karakter kümeleri hakkında bilinmez. –

+1

@Michael Konietzka: Bence bu gereksiz bir nitelendirme. Base64, bir bayt dizisini kodlar. Bence OP, açık bir şekilde, bayt dizisinin bir unicode dizgisinin UTF-8 kodlaması olarak kabul edildiğini söylemiyor ** değil ** 'java.lang.String' doğrudan Base64 olarak kodlanmıştır (mantıklı.) – finnw

+0

@finnw Üzgünüm açık bir şekilde nasıl açıklayacağımı bilmiyorum. Base64 kullanarak kodlanmış string alıyorum ve doğru olup olmadığını kontrol etmek istiyorum. Ben kod çözme sonra çöp gibi görünüyor base64 kodlu dize aldığınızda durum yakalamak istiyorum, çünkü aldığım her şey örnek adı için olmalıdır. – terry207

cevap

18

kullanıyorum.

byte[] bytes = string.getBytes("UTF-8"); 
// feed bytes to Base64 

ve Aksi platformu varsayılan kodlama zorunlu UTF-8 başına olmadığı kullanılacak

// get bytes from Base64 
String string = new String(bytes, "UTF-8"); 

.

+0

Bu dize, tek bayt kodlama olarak yanlış yorumlanmış UTF8 gibi görünmüyor. UTF8 olarak yanlış yorumlanmış GB18030 olabilir mi? – finnw

+0

@finnw: Yanıt, gerçekten de, OP tarafından açıkça belirtildiği gibi, orijinal dizgenin *** *** UTF-8 olduğunu varsayar. Eğer durum bu değilse, sorun başka bir yerde çözülecektir. – BalusC

+0

@ ButalC: Bir String UTF8 ile ne demek istiyorsun? UTF-8 bir kodlamadır. –

1

bu deneyin:

var B64 = { 
    alphabet: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/=', 
    lookup: null, 
    ie: /MSIE /.test(navigator.userAgent), 
    ieo: /MSIE [67]/.test(navigator.userAgent), 
    encode: function (s) { 
     var buffer = B64.toUtf8(s), 
      position = -1, 
      len = buffer.length, 
      nan1, nan2, enc = [, , , ]; 
     if (B64.ie) { 
      var result = []; 
      while (++position < len) { 
       nan1 = buffer[position + 1], nan2 = buffer[position + 2]; 
       enc[0] = buffer[position] >> 2; 
       enc[1] = ((buffer[position] & 3) << 4) | (buffer[++position] >> 4); 
       if (isNaN(nan1)) enc[2] = enc[3] = 64; 
       else { 
        enc[2] = ((buffer[position] & 15) << 2) | (buffer[++position] >> 6); 
        enc[3] = (isNaN(nan2)) ? 64 : buffer[position] & 63; 
       } 
       result.push(B64.alphabet[enc[0]], B64.alphabet[enc[1]], B64.alphabet[enc[2]], B64.alphabet[enc[3]]); 
      } 
      return result.join(''); 
     } else { 
      result = ''; 
      while (++position < len) { 
       nan1 = buffer[position + 1], nan2 = buffer[position + 2]; 
       enc[0] = buffer[position] >> 2; 
       enc[1] = ((buffer[position] & 3) << 4) | (buffer[++position] >> 4); 
       if (isNaN(nan1)) enc[2] = enc[3] = 64; 
       else { 
        enc[2] = ((buffer[position] & 15) << 2) | (buffer[++position] >> 6); 
        enc[3] = (isNaN(nan2)) ? 64 : buffer[position] & 63; 
       } 
       result += B64.alphabet[enc[0]] + B64.alphabet[enc[1]] + B64.alphabet[enc[2]] + B64.alphabet[enc[3]]; 
      } 
      return result; 
     } 
    }, 
    decode: function (s) { 
     var buffer = B64.fromUtf8(s), 
      position = 0, 
      len = buffer.length; 
     if (B64.ieo) { 
      result = []; 
      while (position < len) { 
       if (buffer[position] < 128) result.push(String.fromCharCode(buffer[position++])); 
       else if (buffer[position] > 191 && buffer[position] < 224) result.push(String.fromCharCode(((buffer[position++] & 31) << 6) | (buffer[position++] & 63))); 
       else result.push(String.fromCharCode(((buffer[position++] & 15) << 12) | ((buffer[position++] & 63) << 6) | (buffer[position++] & 63))); 
      } 
      return result.join(''); 
     } else { 
      result = ''; 
      while (position < len) { 
       if (buffer[position] < 128) result += String.fromCharCode(buffer[position++]); 
       else if (buffer[position] > 191 && buffer[position] < 224) result += String.fromCharCode(((buffer[position++] & 31) << 6) | (buffer[position++] & 63)); 
       else result += String.fromCharCode(((buffer[position++] & 15) << 12) | ((buffer[position++] & 63) << 6) | (buffer[position++] & 63)); 
      } 
      return result; 
     } 
    }, 
    toUtf8: function (s) { 
     var position = -1, 
      len = s.length, 
      chr, buffer = []; 
     if (/^[\x00-\x7f]*$/.test(s)) while (++position < len) 
     buffer.push(s.charCodeAt(position)); 
     else while (++position < len) { 
      chr = s.charCodeAt(position); 
      if (chr < 128) buffer.push(chr); 
      else if (chr < 2048) buffer.push((chr >> 6) | 192, (chr & 63) | 128); 
      else buffer.push((chr >> 12) | 224, ((chr >> 6) & 63) | 128, (chr & 63) | 128); 
     } 
     return buffer; 
    }, 
    fromUtf8: function (s) { 
     var position = -1, 
      len, buffer = [], 
      enc = [, , , ]; 
     if (!B64.lookup) { 
      len = B64.alphabet.length; 
      B64.lookup = {}; 
      while (++position < len) 
      B64.lookup[B64.alphabet[position]] = position; 
      position = -1; 
     } 
     len = s.length; 
     while (position < len) { 
      enc[0] = B64.lookup[s.charAt(++position)]; 
      enc[1] = B64.lookup[s.charAt(++position)]; 
      buffer.push((enc[0] << 2) | (enc[1] >> 4)); 
      enc[2] = B64.lookup[s.charAt(++position)]; 
      if (enc[2] == 64) break; 
      buffer.push(((enc[1] & 15) << 4) | (enc[2] >> 2)); 
      enc[3] = B64.lookup[s.charAt(++position)]; 
      if (enc[3] == 64) break; 
      buffer.push(((enc[2] & 3) << 6) | enc[3]); 
     } 
     return buffer; 
    } 
}; 

Görünüm Here

+1

Bu benim için mükemmel çalıştı. Anladığım kadarıyla olumsuz oy aldı çünkü bir java sorusu üzerine javascript yanıtı verdi. –

-1

Ben bu yöntemi yarattı:

public static String descodificarDeBase64(String stringCondificado){ 
    try { 
     return new String(Base64.decode(stringCondificado.getBytes("UTF-8"),Base64.DEFAULT)); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
     return ""; 
    } 
} 

Yani U, A, n, i olarak Base64 ispanyolca characthers gelen çözebilir.

Örnek:

descodificarDeBase64("wr9xdcOpIHRhbD8="); 

döndürür: ¿Que Tal?