Base64Utils加密,解密工具类

写一个Base64Utils工具类:

import org.springframework.util.Base64Utils;


public class Base64Util {
    /**
     * BASE64解密
     *
     * @param key
     * @return
     * @throws Exception
     */
   public static byte[] decryptBASE64(byte[] key) throws Exception {
        return Base64Utils.decode(key);
    }

    /**
     * BASE64加密
     *
     * @param key
     * @return
     * @throws Exception
     */
    public static String encryptBASE64(byte[] key) throws Exception {
        return new String(Base64Utils.encode(key));
    }
}

 

测试方法:

@Test
public void testBase(){
    String b = "测试加密";
    String jiami = "";
    try {
        jiami = Base64Util.encryptBASE64(b.getBytes());
        System.out.println("加密后:" + jiami);
    } catch (Exception e) {
        e.printStackTrace();
    }
    byte[] jm = jiami.getBytes();
    try {
        String jiemi = new String(Base64Util.decryptBASE64(jm), "UTF-8");
        System.out.println("解密后:" + jiemi);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

运行效果:

Base64Utils加密,解密工具类_第1张图片

Base64Utils加密经常用在调用第三方接口的时候,对传输的数据进行加密,在这里记录一下

你可能感兴趣的:(Base64Utils加密,解密工具类)