(1)JDK原生的Base64加解密
public static void JDKCrypto(){
StringencodeStr = Base64.getEncoder().encodeToString(str.getBytes());
System.out.println(encodeStr);
byte[]decodeStr = Base64.getDecoder().decode(encodeStr);
Stringstr = new String(decodeStr);
System.out.println(str);
}
说明:这是使用JDK1.8的版本,低于1.8没有这样的方法。
(2)使用commonsCodes进行加解密
public static void commonsCodesBase64(){
byte[]encodeByte =org.apache.commons.codec.binary.Base64.encodeBase64(str.getBytes());
System.out.println(encodeByte);
byte[]decodeByte = org.apache.commons.codec.binary.Base64.decodeBase64(encodeByte);
System.out.println(newString(decodeByte));
}
说明:需要引入commonsCodes的jar包,下面是maven依赖的坐标。
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
(3)使用BoundcyCastle进行加解密
public static void BoundcyCastleBase64(){
byte[]encodeByte = org.bouncycastle.util.encoders.Base64.encode(str.getBytes());
System.out.println(encodeByte);
byte[]decodeByte = org.bouncycastle.util.encoders.Base64.decode(encodeByte);
System.out.println(newString(decodeByte));
}
说明:需要引入BouncyCastle的jar包,如下是maven的坐标
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.59</version>
</dependency