2014-09-04 118 views
0

我試圖做到的,是檢測的base64字符串的...我已經試過以下接近,但沒有工作了所有字符串。Android的Base64編碼字符串檢測

受審APPROACH

搜索:android.util.Base64包,但它也沒有提供任何功能來檢測的base64字符串... !!!

受審APPROACH

使用正則表達式這是高度upvoted#2而不是工作! ([A-Za-z0-9 + /] {4})*([A-Za-z0-9 + /] {4} | [A-Za-z0-9 + /]} { 3} = | [A-ZA-Z0-9 +/{2} ==)$

它接受 「剪輯」 字符串爲base64即使它不是...

TRIED APPROACH

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

即使這也有同樣的問題....!

受審APPROACH

使用最新的公地編解碼器庫(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 2014-11-11 06:59:32