Skip to content

Instantly share code, notes, and snippets.

@XDo0
Last active December 27, 2021 07:49
Show Gist options
  • Save XDo0/4dae7ad6dc97064d48523cf9ec44c08f to your computer and use it in GitHub Desktop.
Save XDo0/4dae7ad6dc97064d48523cf9ec44c08f to your computer and use it in GitHub Desktop.
java Base64 method
import java.util.Base64;
public class MyBase64{
public static String base64(String str,int mode) throws InvalidParameterException{
if (mode == 1) {
byte[] bytes = str.getBytes();
//Base64 加密
String encoded = Base64.getEncoder().encodeToString(bytes);
return encoded;
} else if (mode == 2) {
//Base64 解密
byte[] decoded = Base64.getDecoder().decode(str);
String decodeStr = new String(decoded);
return decodeStr;
}else {
throw new InvalidParameterException();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment