2014-09-04 2 views
0

내가 달성하기 위해 노력하고있어 base64로 문자열의 감지 ... 난 접근 다음을 시도했지만, 모든 문자열을 위해 일하지 않았다이다.안드로이드 Base64로 문자열 탐지

려고 시도 접근 방식 : android.util.Base64 패키지 검색

, 그러나 또한 base64로 문자열을 감지하는 기능 중 하나를 제공하지 않습니다 ...!

려고 시도 접근 방식 :

고도의 유래에 upvoted하지만 작동되지

사용 정규식! ([A-Za-z0-9 + /] {4}) * ([A-Za-z0-9 + /] {4} | [A-Za-z0-9 + /] { 3} = | [A-ZA-Z0-9 + /]가 아닌 경우에도 64 기수로 {2} ==) $

그것은 받아 들인다 "클립"문자열 ...

시도 접근 방식 :

([A-ZA-Z0-9 +/{4}?) (* : [A-ZA-Z0-9 +/{2} == | [A- Za-z0-9 +/- {3} = | [A-Za-z0-9 +/- {4})

심지어 이것에도 같은 문제가 있습니다 .... !!

려고 시도 접근 방식 :

사용 최신 평민 코덱 라이브러리 (버전 1.9) ...하지만, 추천 된 isBase64 (문자열 STR() 메소드의 그것에 어떠한 방법 해상력이 없다 구글 검색에 많은 peaople에 의해) .. !!

이 문제를 해결하기 위해 많은 검색을했지만 작동하지 않는 답변이 발견되었습니다 ... !!

누구든지이 문제를 해결하고 모든 문자열 집합에 대해 해결책을 찾도록 도와 줄 수 있습니까?

답변

0

그것은 나를 위해 일한이 코드

public class Base64 { 

private Base64() { 
    super(); 
} 

/** 
* Encode some data and return a String. 
*/ 
public final static String encode(byte[] d) { 
    if (d == null) 
     return null; 
    byte data[] = new byte[d.length + 2]; 
    System.arraycopy(d, 0, data, 0, d.length); 
    byte dest[] = new byte[(data.length/3) * 4]; 

    // 3-byte to 4-byte conversion 
    for (int sidx = 0, didx = 0; sidx < d.length; sidx += 3, didx += 4) { 
     dest[didx] = (byte) ((data[sidx] >>> 2) & 077); 
     dest[didx + 1] = (byte) ((data[sidx + 1] >>> 4) & 017 | (data[sidx] << 4) & 077); 
     dest[didx + 2] = (byte) ((data[sidx + 2] >>> 6) & 003 | (data[sidx + 1] << 2) & 077); 
     dest[didx + 3] = (byte) (data[sidx + 2] & 077); 
    } 

    // 0-63 to ascii printable conversion 
    for (int idx = 0; idx < dest.length; idx++) { 
     if (dest[idx] < 26) 
      dest[idx] = (byte) (dest[idx] + 'A'); 
     else if (dest[idx] < 52) 
      dest[idx] = (byte) (dest[idx] + 'a' - 26); 
     else if (dest[idx] < 62) 
      dest[idx] = (byte) (dest[idx] + '0' - 52); 
     else if (dest[idx] < 63) 
      dest[idx] = (byte) '+'; 
     else 
      dest[idx] = (byte) '/'; 
    } 

    // add padding 
    for (int idx = dest.length - 1; idx > (d.length * 4)/3; idx--) { 
     dest[idx] = (byte) '='; 
    } 
    return new String(dest); 
} 

/** 
* Encode a String using Base64 using the default platform encoding 
**/ 
public final static String encode(String s) { 
    return encode(s.getBytes()); 
} 

/** 
* Decode data and return bytes. 
*/ 
public final static byte[] decode(String str) { 
    if (str == null) 
     return null; 
    byte data[] = str.getBytes(); 
    return decode(data); 
} 

/** 
* Decode data and return bytes. Assumes that the data passed in is ASCII 
* text. 
*/ 
public final static byte[] decode(byte[] data) { 
    int tail = data.length; 
    while (data[tail - 1] == '=') 
     tail--; 
    byte dest[] = new byte[tail - data.length/4]; 

    // ascii printable to 0-63 conversion 
    for (int idx = 0; idx < data.length; idx++) { 
     if (data[idx] == '=') 
      data[idx] = 0; 
     else if (data[idx] == '/') 
      data[idx] = 63; 
     else if (data[idx] == '+') 
      data[idx] = 62; 
     else if (data[idx] >= '0' && data[idx] <= '9') 
      data[idx] = (byte) (data[idx] - ('0' - 52)); 
     else if (data[idx] >= 'a' && data[idx] <= 'z') 
      data[idx] = (byte) (data[idx] - ('a' - 26)); 
     else if (data[idx] >= 'A' && data[idx] <= 'Z') 
      data[idx] = (byte) (data[idx] - 'A'); 
    } 

    // 4-byte to 3-byte conversion 
    int sidx, didx; 
    for (sidx = 0, didx = 0; didx < dest.length - 2; sidx += 4, didx += 3) { 
     dest[didx] = (byte) (((data[sidx] << 2) & 255) | ((data[sidx + 1] >>> 4) & 3)); 
     dest[didx + 1] = (byte) (((data[sidx + 1] << 4) & 255) | ((data[sidx + 2] >>> 2) & 017)); 
     dest[didx + 2] = (byte) (((data[sidx + 2] << 6) & 255) | (data[sidx + 3] & 077)); 
    } 
    if (didx < dest.length) { 
     dest[didx] = (byte) (((data[sidx] << 2) & 255) | ((data[sidx + 1] >>> 4) & 3)); 
    } 
    if (++didx < dest.length) { 
     dest[didx] = (byte) (((data[sidx + 1] << 4) & 255) | ((data[sidx + 2] >>> 2) & 017)); 
    } 
    return dest; 
} 

을 시도

+0

base64로 문자열 감지 코드를 제공하십시오 ... 코드 당신이 검출이없는 준 ??? – Developer