最近自己写了个用BASE64编码“加密”的小java类,觉得有点意思,发出来一下,希望大家不要见笑
/*author: hujinpu
*
*用于编码BASE64t和解码BASE64
*由于用了Scanner类,所以要求JDK1.5或更高版本
*/
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!");
}
}
}
*
*用于编码BASE64t和解码BASE64
*由于用了Scanner类,所以要求JDK1.5或更高版本
*/
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:hujinpu
@echo off
set CLASSPATH = 你放BASE64 . java的目录 ; %CLASSPATH%
java BASE64 %1
del %1
@echo off
set CLASSPATH = 你放BASE64 . java的目录 ; %CLASSPATH%
java BASE64 %1
del %1