Last active
December 27, 2021 07:49
-
-
Save XDo0/4dae7ad6dc97064d48523cf9ec44c08f to your computer and use it in GitHub Desktop.
java Base64 method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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