Base 64符号化の紹介とJava言語に基づく実装


Base 64符号化の紹介とJava言語に基づく実装
 
Base 64符号化は、任意の順序の8ビットの1バイト単位を表すように設計され、コンテンツの読み取りを必要とせずに大文字および小文字を使用することができる.65個のUS-ACSCIIサブセットはBase 64符号化を表すために使用されるので、6ビットごとに印刷可能なBase 64の文字を表し、65番目の文字'='は特殊な処理の機能文字を表すために使用され、後述する.
 
プリコーディング処理は、一度に24ビット(3文字)を一組として入力し、4つのBase 64の符号化文字24ビット(各文字6ビット)を出力する.符号化処理は左から右の順であり、1つの24ビットの入力は3つの8ビットの文字群と見なすことができる.
符号化後、24ビットの文字は4つの6ビットの文字グループに変換され、各文字はBase 64文字テーブルの一致する1文字に翻訳される.
 
6ビットごとにindex値に変換され、Base 64の文字テーブルの1文字出力に一致します.Base 64の文字テーブルは次のようになります.
Value Encoding Value Encoding Value Encoding Value Encoding
0 A                  17 R              34 i      51 z
1 B                  18 S              35 j       52 0
2 C                  19 T              36 k      53 1
3 D                  20 U              37 l       54 2
4 E                  21 V              38 m       55 3
5 F                  22 W              39 n       56 4
6 G                  23 X              40 o       57 5
7 H                  24 Y              41 p       58 6
8 I                  25 Z              42 q       59 7
9 J                  26 a              43 r       60 8
10 K                 27 b              44 s       61 9
11 L                 28 c              45 t       62 +
12 M                 29 d              46 u       63/
13 N                 30 e              47 v
14 O                 31 f              48 w       (pad) =
15 P                  32 g             49 x
16 Q                 33 h              50 y
 
 
文字'='は、パディング文字として、24を入力としない符号化文字に対して、0をパディングとして24ビット(3バイト)入力とし、出力の符号化文字が24ビット未満のものに対しては'='文字でパディングしなければならない.次の場合:
Ø        最後に入力がちょうど24ビットの符号化文字であれば、′=′文字の埋め込みを必要とせず、出力は4つのBase 64文字の符号化となる.
Ø        最後に8ビットのみの符号化文字を入力と、2つの特殊文字'='、出力は2つのBase 64文字の符号化に2つの'='を加える必要がある.
Ø        最後に16ビットのみの符号化文字を入力と、特殊文字'='、出力は3つのBase 64文字に1つの'='を加える必要がある.
  
1つのバイナリのデータをBase 64の符号化に変換し、次の例ではBase 64が3バイトのバイナリデータをどのように符号化するかを説明します.
+--first octet--+-second octet--+--third octet--+
|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|7 6 5 4 3 2 1 0|
+-----------+---+-------+-------+---+-----------+
|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|5 4 3 2 1 0|
+--1.index--+--2.index--+--3.index--+--4.index--+
 
多くのネットワークプログラムでバイナリファイルを転送するために使用されるBase 64の符号化アルゴリズムもよく採用されており、最も典型的な例はEmailのアプリケーションプロトコルSMPTがMIMEデータを送信する際にBase 64を用いて送信されることが多く、標準のRFCに書き込まれており、Java言語ではJDKがBase 64のAPIを提供している.
 
以下、Javaに基づいて実装されるBase 64の符号化と復号化プログラムは、JDK 6でBase 64の符号化と復号化を2つの異なるAPI Classに分けているので、多くの人を酔わせているので、本人の実装は少し便利かもしれません.
package com.gloomyfish.smtp.util;


public class Base64Coder {
	
	public final static char[] base64_alphabet = new char[]{
            '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','+','/','='
	};
	
	public static String encode(String content) {
		
		byte[] data = content.getBytes();
		int length = data.length;
		byte[] char_array_3 = new byte[]{0, 0, 0};
		byte[] char_array_4 = new byte[]{'=','=','=','='};
		String retContent = "";
		int i = 0;
		int j = 0;
		int reversePos = 0;
		while(length > 0) {
			length--;
			char_array_3[i++] = data[reversePos++];
			if(i==3) {
				char_array_4[0] = (byte)((char_array_3[0] & 0xfc) >> 2); // convert the char
			    char_array_4[1] = (byte)(((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4));
			    char_array_4[2] = (byte)(((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6));
			    char_array_4[3] = (byte)(char_array_3[2] & 0x3f);
			    for(i = 0; (i <4) ; i++)
			    	retContent += base64_alphabet[char_array_4[i]];
			    i = 0;
			}
		}
		
		// handling the last input content
		  if (i > 0 )
		  {
		    for(j = i; j < 3; j++)
		      char_array_3[j] = 0; // padding of zero

		    char_array_4[0] = (byte)((char_array_3[0] & 0xfc) >> 2); // right shift
		    char_array_4[1] = (byte)(((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4));
		    char_array_4[2] = (byte)(((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6));
		    char_array_4[3] = (byte)(char_array_3[2] & 0x3f);

		    for (j = 0; (j < i + 1); j++)
		    	retContent += base64_alphabet[char_array_4[j]];

		    while((i++ < 3)) // padding of '=' of output string
		    	retContent += '=';

		  }
		return retContent;
	}
	
	public static String decode(String enContent) {
		byte[] data = enContent.getBytes();
		int i = 0, j = 0, enCode = 0;
		int mLength = data.length;
		byte[] char_array_4 = new byte[4];
		byte[] char_array_3 = new byte[3];
		String retContent = "";

		// filter out the padding '=' chars
		  while (mLength > 0 && (((char)data[enCode]) != '=') && isBase64((char)data[enCode])) 
			{
			  mLength--;
			  char_array_4[i++] = data[enCode++];
			  if (i ==4) {
		      for (i = 0; i <4; i++)
		        char_array_4[i] = findChar((char)char_array_4[i]);

		      char_array_3[0] = (byte)((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
		      char_array_3[1] = (byte)(((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
		      char_array_3[2] = (byte)(((char_array_4[2] & 0x3) << 6) + char_array_4[3]);

		      for (i = 0; (i < 3); i++)
		    	  retContent += (char)char_array_3[i];
		      i = 0;
		    }
		  }

		  // last content handling
		  if (i > 0) 
			{
		    for (j = i; j <4; j++)
		      char_array_4[j] = 0;

		    for (j = 0; j <4; j++)
		      char_array_4[j] = findChar((char)char_array_4[j]);

		    char_array_3[0] = (byte)((char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4));
		    char_array_3[1] = (byte)(((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2));
		    char_array_3[2] = (byte)(((char_array_4[2] & 0x3) << 6) + char_array_4[3]);

		    for (j = 0; (j < i - 1); j++) 
		    	retContent += (char)char_array_3[j];
		  }

		  return retContent;
	}
	
	public static boolean isBase64(char c) 
	{
		boolean base64 = false;
		for(int i=0; i<64; i++) {
			if( c == base64_alphabet[i]) {
				base64 = true;
				break;
			}
		}
	  return base64;
	}
	
	public static byte findChar(char x) {
		byte index = 64; // 65th char '='
		for(int i=0; i<64; i++) {
			if( x == base64_alphabet[i]) {
				index = (byte)i;
				break;
			}
		}
		return index;
	}
	
	/**
	 * <p> test data and result should like below output , RFC4648 Sample </p>
	 * 	BASE64("") = ""
	 *	BASE64("f") = "Zg=="
	 *	BASE64("fo") = "Zm8="
	 *	BASE64("foo") = "Zm9v"
	 *	BASE64("foob") = "Zm9vYg=="
	 *	BASE64("fooba") = "Zm9vYmE="
	 *	BASE64("foobar") = "Zm9vYmFy"
	 *
	 *
	 * @param args
	 */
	public static void main(String[] args) {
		// BASE64Encoder coder = new BASE64Encoder();
		// System.out.println(coder.encode("foobar".getBytes()));
		
		System.out.println("#--------------encode---------------#");
		System.out.println(encode(""));
		System.out.println(encode("f"));
		System.out.println(encode("fo"));
		System.out.println(encode("foo"));
		System.out.println(encode("foob"));
		System.out.println(encode("fooba"));
		System.out.println(encode("foobar"));
		System.out.println(encode("123456789sS{1}quot;));
		System.out.println("#--------------decode---------------#");
		System.out.println(decode(""));
		System.out.println(decode("Zg=="));
		System.out.println(decode("Zm8="));
		System.out.println(decode("Zm9v"));
		System.out.println(decode("Zm9vYg=="));
		System.out.println(decode("Zm9vYmE="));
		System.out.println(decode("Zm9vYmFy"));
		System.out.println(decode("MTIzNDU2Nzg5c1Mk"));
		
	}

}