Base 64復号化


概要:base 64復号化
英語文字のbase 64を復号化し、非英語文字は符号化の問題のため、本明細書ではサポートされていない.
base 64の暗号化プロセス規則は、1.文字列の各文字を、対応するASCII符号値のバイナリ表現に変換し、各文字のバイナリ長を8ビットとし、バイナリ文字列を得る.    2.得られたバイナリ文字列の長さが6の整数倍でない場合、末尾に0を追加し、6の倍数にする.    3.ステップ2で得られた文字列では、6ビット毎に切り取り、その対応する数値は0-63であり、base 64変換テーブルから対応する文字([A-Za-z 0-9/+])を検索し、文字列に再結合する.    4.ステップ3で得られた文字列の長さが4の倍数でない場合、末尾に等号("=")を4の倍数に追加します.1文字暗号化後は2文字、2文字暗号化後は3文字、3文字暗号化後は4文字…8と6の最小公倍数は24なので、等号個数は0、1、2個しかありません.
コードは次のとおりです.
import java.util.HashMap;
import java.util.Map;

public class Base64 {
    private static char[] base64 = {'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', '+', '/'};
    private static Map deBase64 = new HashMap(
            base64.length);

    static {
        for (int index = 0; index < base64.length; index++) {
            deBase64.put(base64[index], index);
        }
    }

    /**
     * base64  
     * 
     * @param input
     *                  
     * @return       
     */
    public static String encrypt(String input) {
        StringBuilder output = new StringBuilder();
        StringBuilder binString = new StringBuilder();
        for (int index = 0; index < input.length(); index++) {
            binString.append(getFixLenBin(input.charAt(index), 8));
        }
        //                0
        while (binString.length() % 6 != 0) {
            binString.append("0");
        }
        for (int index = 0; index < binString.length(); index += 6) {
            output.append(
                    base64[bin2Num(binString.substring(index, index + 6))]);
        }
        while (output.length() % 4 != 0) {
            output.append("=");
        }
        return output.toString();
    }

    /**
     *            
     * 
     * @param num
     *                
     * @param len
     *              
     * @return          
     */
    private static String getFixLenBin(int num, int len) {
        String bin = num2bin(num);
        while (bin.length() < len) {
            bin = "0".concat(bin);
        }
        return bin;
    }

    /**
     *        
     * 
     * @param num
     *                
     * @return       
     */
    private static String num2bin(int num) {
        StringBuilder sb = new StringBuilder();
        String bin = getBin(num / 2, sb, num % 2);
        bin = bin.replaceAll("^[0]*", "");
        return bin.length() != 0 ? bin : "0";
    }

    /**
     *                   
     * 
     * @param num
     *                /2
     * @param sb
     *                  
     * @param mod
     *                %2
     * @return          
     */
    private static String getBin(int num, StringBuilder sb, int mod) {
        sb.append(mod);
        return num / 2 == 0
                ? sb.append(num % 2).reverse().toString()
                : getBin(num / 2, sb, num % 2);
    }

    /**
     *        
     * 
     * @param bin
     *                  
     * @return     
     */
    private static int bin2Num(String bin) {
        int num = 0;
        int len = bin.length();
        for (int index = 0; index < len; index++) {
            if (bin.charAt(len - 1 - index) == '1') {
                num += Math.pow(2, index);
            }
        }
        return num;
    }

    /**
     * base64  
     * 
     * @param input
     *                  
     * @return       
     */
    public static String decrypt(String input) {
        input = input.replaceAll("[^A-Za-z0-9+/=]*", "");
        //      4   
        if (input.length() % 4 != 0) {
            return "      :    ";
        }
        //        0、1、2 ,      0、3、2,     1      ,   base64   
        input = input.replaceAll("=", "");
        if (input.length() % 4 == 1) {
            return "      :      ";
        }

        StringBuilder binString = new StringBuilder();
        for (int index = 0; index < input.length(); index++) {
            binString.append(getFixLenBin(
                    deBase64.get(input.charAt(index)).intValue(), 6));;
        }
        StringBuilder output = new StringBuilder();
        int len = binString.length();
        for (int index = 0; index < len; index += 8) {
            //          0
            if (index + 8 > len) {
                break;
            }
            output.append(
                    (char) bin2Num(binString.substring(index, index + 8)));
        }
        return output.toString();
    }
}