package com.imooc.security.base64;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* Base64加密算法
* @author Bingo_Ge
*
*/
public class ImoocBase64 {
private static String src = "imooc security base64";
public static void main(String[] args) {
jdkBase64();
commonsCodesBase64();
bouncyCastleBase64();
}
private static void jdkBase64() {
BASE64Encoder encoder = new BASE64Encoder();
String encode = encoder.encode(src.getBytes());
System.out.println("jdk encode:" + encode);
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] decodeBytes = decoder.decodeBuffer(encode);
System.out.println("jdk decode:" + new String(decodeBytes));
} catch (IOException e) {
e.printStackTrace();
}
}
private static void commonsCodesBase64() {
byte[] encodeBytes = Base64.encodeBase64(src.getBytes());
System.out.println("cc encode:" + new String(encodeBytes));
byte[] decodeBytes = Base64.decodeBase64(encodeBytes);
System.out.println("cc decode:" + new String(decodeBytes));
}
private static void bouncyCastleBase64() {
byte[] encodeBytes = org.bouncycastle.util.encoders.Base64.encode(src.getBytes());
System.out.println("bc encode:" + new String(encodeBytes));
byte[] decodeBytes = org.bouncycastle.util.encoders.Base64.decode(encodeBytes);
System.out.println("bc decode:" + new String(decodeBytes));
}
}
Base64加密算法
最新推荐文章于 2023-09-07 09:40:04 发布