1.JDK
sun.misc.BASE64Encoder/BASE64Decoder类不属于JDK标准库范畴,但在JDK中包含了该类,可以直接使用。
测试:
import org.junit.Test;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import java.io.IOException;
public class Base64Test {
private String src="/upload/123.jsp";
@Test
public void JDKBase64(){
//加密
BASE64Encoder encoder = new BASE64Encoder();
String encode = encoder.encode( src.getBytes() );
System.out.println( "JDK--encode:" + encode );
//解密
BASE64Decoder decoder = new BASE64Decoder();
try {
byte[] decode = decoder.decodeBuffer( encode );
System.out.println( "JDK--decode:" + new String( decode ) );
} catch( IOException e ) {
e.printStackTrace();
}
}
}
结果:
2.commons-codec
导入依赖:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>
测试:
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
public class Base64Test {
private String src="/upload/123.jsp";
@Test
public void commonsCodecBase64() {
//加密
byte[] encode = Base64.encodeBase64( src.getBytes() );
System.out.println( "commons--encode:" + new String( encode ) );
//解密
byte[] decode = Base64.decodeBase64( encode );
System.out.println( "commons--decode:" + new String( decode ) );
}
}
结果:
3.bouncycastle
导入依赖:
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15</artifactId>
<version>1.46</version>
</dependency>
测试:
import org.junit.Test;
public class Base64Test {
private String src="/upload/123.jsp";
@Test
public void bouncyCastleBase64() {
//加密
byte[] encode = org.bouncycastle.util.encoders.Base64.encode( src.getBytes() );
System.out.println( "bouncycastle--encode:" + new String( encode ) );
//解密
byte[] decode = org.bouncycastle.util.encoders.Base64.decode( encode );
System.out.println( "bouncycastle--decode:" + new String( decode ) );
}
}
结果:
评论 (0)