用到的包
import java.util.Base64;
将字符串进行加密
public static String encrypt(String str) {
String str_encrypt = null;
byte[] bytes = str.getBytes();
//if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
str_encrypt = Base64.getEncoder().encodeToString(bytes);
//}
return str_encrypt;
}
如果需要判断sdk则取消注释即可
解密
public static String decrypt(String str) {
if (str == null) return null;
String str_decrypt = null;
//if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
byte[] bytes = Base64.getDecoder().decode(str);
str_decrypt = new String(bytes);
// }
return str_decrypt;
}
如果传入为空,则直接返回空