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("[email protected]");
System.out.println(str);
}
}