BASE64编码与解码

时间:2023-01-10 18:01:10

实现方式:
* 1.jdk7及以下自带的sun.misc.*
* 2.jdk8自带的java.util.Base64;
* 3.外界的包commons-codec-1.5.jar

1.jdk8java.util.Base64工具类提供了一套静态方法获取下面三种BASE64编解码器:
1)Basic编码
2)URL编码
3)MIME编码
2.jdk7及以下BASE64编解码器:
Basic编码和MIME编码(格式化)
这里演示的是jdk7的BASE64编码与解码
Base64可作一个简单的“加密”来保护某些数据,而真正的加密通常都比较繁琐;

在使用之前需要解除访问限制:
在java Bulid Path的jre下面加*问许可的范围sun/misc/*即可;
第一部分:实现字符串的编码与解码

public class BASE64Demo1 {
public static String encode(String s) {
if (s!=null) {
try {
return encode(s.getBytes("UTF-8"));//默认中文编码,设置为UTF-8
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return null;
}
//编码
public static String encode(byte by[]) {
if (by!=null) {
BASE64Encoder base=new BASE64Encoder();
return base.encode(by);
}
return null;
}

//解码
public static String decode(String str) throws IOException {
if (str!=null) {
BASE64Decoder dec=new BASE64Decoder();
return new String(dec.decodeBuffer(str),"UTF-8");
}
return null;
}

// baseEncode
public static void main(String[] args) throws Exception {
// String s=encode(" ");
String s=encode("subjects?abcd");
System.out.println("编码:"+s);
System.out.println("解码:"+decode(s));
}
}

第二部分:
实现:
* 1.将字符串加密到文本,将文本信息解压到控制台
* 2.将文本加密到另外一个文本,将文本解密到另一个文本

public class BASE64Demo2 {

// 1.将字符串加密到文本,将文本信息解密到控制台
public static void encode(String str, File destFile) {
if (!destFile.isFile()) { // 包括了文件不存在情况
System.err.println("请输入正确的源文件路径!");
return;
}

BASE64Encoder encode = new BASE64Encoder();
try {
if (str != null) {
encode.encode(str.getBytes("UTF-8"), new FileOutputStream(destFile));
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("ok");
}

// 将文本加密到另外一个文本
public static void encode(File srcFile, File destFile) {
if (!srcFile.isFile()) { // 包括了文件不存在情况
System.err.println("请输入正确的源文件路径!");
return;
}
BASE64Encoder encode = new BASE64Encoder();
try {
//76个字符自动换行,跟jdk8的MimeEncoder()编码效果一样
encode.encode(new FileInputStream(srcFile), new FileOutputStream(destFile));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("ok");
}

//解码到控制台
public static void decode(File encodeFile) {
if (!encodeFile.isFile()) { // 包括了文件不存在情况
System.err.println("请输入正确的源文件路径!");
return;
}
BASE64Decoder decode=new BASE64Decoder();
try {
byte bys[]=decode.decodeBuffer(new FileInputStream(encodeFile));
System.out.println(new String(bys,"UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("ok");
}

//解码到另一个文件
public static void decode(File encodeFile, File decodeFile) {
if (!encodeFile.isFile()) { // 包括了文件不存在情况
System.err.println("请输入正确的源文件路径!");
return;
}

BASE64Decoder decode=new BASE64Decoder();
try {
decode.decodeBuffer(new FileInputStream(encodeFile), new FileOutputStream(decodeFile));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("ok");
}

public static void main(String[] args) {
// encode("哈哈哈", new File("src/com/BASE64/test1.txt"));
// decode(new File("src/com/BASE64/test1.txt"));

File file1=new File("src/com/BASE64/test1.txt");
File file2=new File("src/com/BASE64/test2.txt");
File file3=new File("src/com/BASE64/test3.txt");
// encode(file1, file2);
// decode(file2, file3);
}
}