JDK 新特性之Base64加密和解密

jdk1.8新增长了Base64加密和解密的处理,不须要引入别的包就能够实现加密

直接上代码code

public static void main(String[] args) { try { String hello1 = "hello,你好!"; byte[] bytes = hello1.getBytes("utf-8"); System.out.println("加密前:"+hello1); String enStr = Base64.getEncoder().encodeToString(bytes); System.out.println("加密后:"+enStr); String hello2 = new String(Base64.getDecoder().decode(enStr),"utf-8"); System.out.println("解密后:"+hello2); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }utf-8

这是运行结果get

加密前:hello,你好! 加密后:aGVsbG8s5L2g5aW977yB 解密后:hello,你好!io

相关文章
相关标签/搜索