1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
| package msdev.test.util; import org.apache.commons.lang3.StringUtils; import java.util.regex.Matcher; import java.util.regex.Pattern;
public class Desensitization {
public static String idCardDesensitization(String idCard) { if (StringUtils.isNotEmpty(idCard)) { if (idCard.length() == 15) { idCard = idCard.replaceAll("(\\w{6})\\w*(\\w{3})", "$1******$2"); } else if (idCard.length() == 18) { idCard = idCard.replaceAll("(\\w{6})\\w*(\\w{3})", "$1*********$2"); } } return idCard; }
public static String mobilePhoneDesensitization(String mobilePhone) { if (StringUtils.isNotEmpty(mobilePhone)) { mobilePhone = mobilePhone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"); } return mobilePhone; }
public static String emailDesensitization(String email) { if (StringUtils.isEmpty(email)) { return email; } String encrypt = email.replaceAll("(\\w+)\\w{3}@(\\w+)", "$1***@$2"); if (email.equalsIgnoreCase(encrypt)) { encrypt = email.replaceAll("(\\w*)\\w{1}@(\\w+)", "$1*@$2"); } return encrypt; }
public static String acctNoDesensitization(String acctNo) { if (StringUtils.isNotEmpty(acctNo)) { String regex = "(\\w{6})(.*)(\\w{4})"; Matcher m = Pattern.compile(regex).matcher(acctNo); if (m.find()) { String rep = m.group(2); StringBuilder sb = new StringBuilder(); for (int i = 0; i < rep.length(); i++) { sb.append("*"); } acctNo = acctNo.replaceAll(rep, sb.toString()); } } return acctNo; }
public static String custNameDesensitization(String custName) { if (StringUtils.isNotEmpty(custName)) { char[] chars = custName.toCharArray(); if (chars.length < 5) { if (chars.length > 1) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < chars.length - 2; i++) { sb.append("*"); } custName = custName.replaceAll(custName.substring(1, chars.length - 1), sb.toString()); } } else { int start = 4; String str1 = custName.substring(0, start); String str2 = ""; if (chars.length == 5) { str2 = "*"; } else if (chars.length == 6) { str2 = "**"; } else if (chars.length == 7) { str2 = "***"; } else if (chars.length == 8) { str2 = "****"; } else if (chars.length == 9) { str2 = "*****"; } else { str2 = "******"; } int subIndex = start + str2.length(); String str3 = custName.substring(subIndex); StringBuffer sb = new StringBuffer(); sb.append(str1); sb.append(str2); sb.append(str3); custName = sb.toString(); } } return custName; }
public static String addressDesensitization(String address) { if (StringUtils.isNotEmpty(address)) { char[] chars = address.toCharArray(); if (chars.length > 11) { String str1 = address.substring(0, 4); String str2 = "********"; String str3 = address.substring(12); StringBuffer sb = new StringBuffer(); sb.append(str1); sb.append(str2); sb.append(str3); address = sb.toString(); } } return address; }
public static final int ONE = 1; public static final int TWO = 2;
public static String desensitizedName(String realName) { if (realName == null) { return null; } if (realName.length() == ONE) { return realName; } else if (realName.length() == TWO) { return realName.substring(0, 1) +"*"; } else { Integer length = realName.length(); StringBuffer middle = new StringBuffer(); for (int i = 0; i < realName.substring(1, length - 1).length(); i++) { middle.append("*"); } return realName.substring(0, 1) + middle + realName.substring(length - 1, length); } }
public static String desensitizedAddress(String address){ if (StringUtils.isNotEmpty(address)) { int length = address.length(); int indes = address.indexOf("区"); if (indes == -1) { indes = address.indexOf("市"); } address = address.substring(0, indes + 1); StringBuffer middle = new StringBuffer(); for (int i = 0; i < length - indes; i++) { middle.append("*"); } return address + middle; } return address; }
public static String desValue(String origin, int prefixNoMaskLen, int suffixNoMaskLen, String maskStr) { if (origin == null) { return null; } StringBuilder sb = new StringBuilder(); for (int i = 0, n = origin.length(); i < n; i++) { if (i < prefixNoMaskLen) { sb.append(origin.charAt(i)); continue; } if (i > (n - suffixNoMaskLen - 1)) { sb.append(origin.charAt(i)); continue; } sb.append(maskStr); } return sb.toString(); }
public static String chineseName(String fullName) { if (fullName == null) { return null; } return desValue(fullName, 0, 1, "*"); }
public static void main(String[] args) { String name = "张三"; String phone = "18866668888";
String dsName = chineseName(name);
String dsPhone = mobilePhoneDesensitization(phone);
System.out.println(dsName); System.out.println(dsPhone);
} }
|