import org.apache.commons.codec.binary.Base64;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.IOException;
public class TestController {
/**
* base64加密的两种实现方式
* jdk/CommonsCodec
* @param arg
*/
public static void main(String[] arg){
String a = "cheng xin na";
jdkBase64(a);
commonsCodecBase64(a);
}
public static void jdkBase64(String str){
try {
BASE64Encoder base64Encoder = new BASE64Encoder();
String result = base64Encoder.encode(str.getBytes());
System.out.println(result);
BASE64Decoder base64Decoder = new BASE64Decoder();
System.out.println(new String(base64Decoder.decodeBuffer(result)));
} catch (IOException e) {
e.printStackTrace();
}
}
public static void commonsCodecBase64(String str){
byte[] result = Base64.encodeBase64(str.getBytes());
System.out.println(new String(result));
byte[] decodeResult = Base64.decodeBase64(result);
System.out.println(new String(decodeResult));
}
}
base64加密的两种实现方式
最新推荐文章于 2024-03-25 20:00:00 发布