package utils;
import java.io.UnsupportedEncodingException;
import java.util.Base64;
public class Base64Utils
{
public static byte[] encode(byte[] bytes)
{
return Base64.getEncoder().encode(bytes);
}
public static byte[] encode(String s)
{
try
{
return encode(s.getBytes("utf-8"));
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return null;
}
public static String encodeToStr(String s)
{
byte[] bytes = encode(s);
return new String(bytes);
}
public static byte[] decode(byte[] bytes)
{
return Base64.getDecoder().decode(bytes);
}
public static byte[] decode(String s)
{
return Base64.getDecoder().decode(s.getBytes());
}
public static String decodeToStr(String s)
{
byte[] bytes = decode(s);
try
{
return new String(bytes, "utf-8");
} catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
return "";
}
}
Java加密篇(1)-Base64
最新推荐文章于 2024-08-23 15:54:50 发布