主要參考了慕課網上《java實現Base64加密》這門課程
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
public class exerciseBase64 {
private static String src="l love programming";
public static void main(String[] args) {
jdkBase64();
commonsCodeBase64();
bouncyCastleBase64();
}
//jdk實現
public static void jdkBase64() {
BASE64Encoder encoder=new BASE64Encoder();
String encode=encoder.encode(src.getBytes());
System.out.println("jdk實現\n加密後:"+encode);
BASE64Decoder decoder=new BASE64Decoder();
try {
System.out.println("解密後:"+new String(decoder.decodeBuffer(encode)));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//cc實現
public static void commonsCodeBase64() {
byte[] encodeBytes=Base64.encodeBase64(src.getBytes());
System.out.println("Commons Codec實現\n加密後:"+new String(encodeBytes));
byte[] decodeBytes=Base64.decodeBase64(encodeBytes);
System.out.println("解密後:"+new String(decodeBytes));
}
//bc實現
public static void bouncyCastleBase64() {
byte[] encodeBytes=org.bouncycastle.util.encoders.Base64.decode(src.getBytes());
System.out.println("Bouncy Castle實現\n加密後:"+new String(encodeBytes));
byte[] decodeBytes=org.bouncycastle.util.encoders.Base64.encode(encodeBytes);
System.out.println("解密後:"+new String(decodeBytes));
}
}