import java.io.UnsupportedEncodingException; import java.nio.charset.StandardCharsets; import java.util.Base64; public class Demo { public static void main(String[] args) throws UnsupportedEncodingException { String str = "hello Base64 啊啊 " + "ADCDEFG"; //编码加密 String encodeStr = Base64.getEncoder().encodeToString(str.getBytes("UTF-8")); System.out.println("加密后的字符串为:" + encodeStr); //解码解密 String decoderStr = new String(Base64.getDecoder().decode(encodeStr), StandardCharsets.UTF_8); // // 推荐使用StandardCharsets类指定 System.out.println("解密后的字符串为" + decoderStr); } }