最近自己寫了個用BASE64編碼“加密”的小java類,覺得有點意思,發出來一下,希望大家不要見笑
/**//*author: livahu
*
*用於編碼BASE64t和解碼BASE64
*
*/
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.io.FileNotFoundException;
import java.util.Scanner;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class BASE64
...{
//將s進行BASE64編碼
public static String getBASE64EncoderStr(String s) throws UnsupportedEncodingException
...{
if (null == s)
return null;
else
return new BASE64Encoder().encode(s.getBytes("GB2312"));
}
//將s進行BASE64解碼
public static byte[] getBASE64DecoderBt(String s) throws IOException
...{
if (null == s)
return null;
else
...{
BASE64Decoder decoder = new BASE64Decoder();
return decoder.decodeBuffer(s);
}
}
//將指定檔案用BASE64編碼
public static void changeToBASE64(String filePath) throws IOException
...{
Scanner sc = new Scanner(new FileInputStream(filePath)).useDelimiter(" ");
PrintWriter pw = new PrintWriter(filePath + "_BASE64_");
while (sc.hasNext())
...{
pw.println(getBASE64EncoderStr(sc.next()));
}
sc.close();
pw.close();
}
//將指定檔案從BASE64解碼
public static void recoverFromBASE64(String filePath) throws IOException, FileNotFoundException
...{
Scanner sc = new Scanner(new FileInputStream(filePath)).useDelimiter(" ");
PrintWriter out = new PrintWriter(filePath.substring(0, filePath.indexOf("_BASE64_")));
while (sc.hasNext())
...{
out.println(new String(getBASE64DecoderBt(sc.next()), "GB2312"));
}
sc.close();
out.close();
}
//main方法
public static void main(String[] args)
...{
try
...{
if (args[0].indexOf("_BASE64_") > 0)
...{
recoverFromBASE64(args[0]);
}
else
...{
changeToBASE64(args[0]);
}
}
catch (IOException e)
...{
System.out.println("Catch some IOException!");
}
}
}
下面是批次檔
rem author:livahu
@echo off
set CLASSPATH=你放BASE64.java的目錄;%CLASSPATH%
java BASE64 %1
del %1
只要把任意檔案往BASE64.bat一拖就好了,呵呵。