Base64加解密

(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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值