在java8之前我们需要用base64时需要引用apache的commons包或第三方jar包来实现,现在内置了api更加的方便,下面一个简单的例子
String password = "123456";
//加密
String encoded = Base64.getEncoder().encodeToString(password.getBytes(StandardCharsets.UTF_8));
//解密
String decoded = new String(Base64.getDecoder().decode( encoded ),StandardCharsets.UTF_8 );
System.out.println(encoded);
System.out.println(decoded);
打印输出:
MTIzNDU2
123456