1. 程式人生 > >BASE64加解密

BASE64加解密

- JDK 自帶

- Commons Codec  地址:http://commons.apache.org/proper/commons-codec/download_codec.cgi

- Bouncy Castle 地址:https://www.bouncycastle.org/latest_releases.html

 



import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import org.apache.commons.codec.binary.Base64;

public class Main3 {


public static void main(String[] args) throws Exception{
String str = "base64";
byte[] bs = Base64.encodeBase64(str.getBytes());
System.out.println(new String(bs));
System.out.println(new String(Base64.decodeBase64(bs)));
BASE64Encoder base64Encoder = new BASE64Encoder();

String encode = base64Encoder.encode(str.getBytes());
System.out.println(encode);
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] bytes = base64Decoder.decodeBuffer(encode);
System.out.println(new String(bytes));

byte[] encode1 = org.bouncycastle.util.encoders.Base64.encode(str.getBytes());

System.out.println(new String(encode));
byte[] decode = org.bouncycastle.util.encoders.Base64.decode(new String(encode1));
System.out.println(new String(decode));
}
}

YmFzZTY0
base64
YmFzZTY0
base64
YmFzZTY0
base64