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);
}
}