class Main
{
public static void main(String[] args)
{
if(args.length<1 || args.length >2)
{
System.out.println("usage : Program string table(option)");
}
else if(args.length==2)
{
base64encode.settable(args[1]);
base64encode.init();
System.out.println(base64encode.encode(args[0]));
}
else
{
base64encode.init();
System.out.println(base64encode.encode(args[0]));
}
}
}
class base64encode
{
static String table_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\\";
static char[] table = new char[64];
static void settable(String chtable)
{
if(chtable.length()!=64)
{
System.out.println("Table must be 64 length\n");
}
else
{
table_string = chtable;
}
}
static void init()
{
for(int i = 0;i<table.length;i++)
{
table[i] = table_string.charAt(i);
}
}
static String encode(String str1)
{
String bin;
String bin2 = "";
String result = "";
int foo;
int padding;
for(int i=0;i<str1.length();i++)
{
bin = Integer.toBinaryString(str1.charAt(i));
while(bin.length()<8)
{
bin = "0" + bin;
}
bin2 = bin2 + bin;
}
if((bin2.length()%24)!=0)
{
padding = 24 - (bin2.length() % 24);
}
else
{
padding = 0;
}
for(int i=0;i<padding;i++)
{
bin2 = bin2 + "0";
}
for(int i=0;i<bin2.length();i=i+6)
{
foo = Integer.parseInt(bin2.substring(i,i+6),2);
if(foo == 0 && i>=bin2.length()-12)
{
result = result + "=";
}
else
{
result = result + table[foo];
}
}
return result;
}
}
일반적인 base64 뿐만 아니라 자기가 테이블을 만들수도 있다. 뭔가 자바는 묵직한 느낌이다