Java使用*字符将敏感信息隐藏
在一些业务场景,我们需要对客户的手机号、邮箱等敏感信息进行隐藏。通常情况下是使用*字符替换原有的字符。以达到加密的目的。
/**
* @author FeianLing
* @date 2019/10/23
* 对字符串敏感信息加密,
*/
public class StringEncryptUtil {
private static final Integer THREE = 3;
public StringEncryptUtil() {
}
/**
* @param
* @param value
* @return java.lang.String
* @author FeianLing
* @date 2019/10/23
* @desc 对敏感信息进行加密,value 3位后面的值全部使用*号代替
*/
public static String encryptAfterThree(String value) {
if (value == null || value.length() <= StringEncryptUtil.THREE) {
return value;
}
char[] arr = value.toCharArray();
Arrays.fill(arr, StringEncryptUtil.THREE, arr.length, '*');
return new String(arr);
}
public static void main(String[] args) {
String str = StringEncryptUtil.encryptAfterThree("13929552209");
System.out.println(str);
str = StringEncryptUtil.encryptAfterThree("Feianling@outlook.com");
System.out.println(str);
}
}