脱敏工具类

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 {
/**
* 身份证号脱敏
*
* @param idCard
* @return
*/
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");
}
// 身份证号脱敏规则二:保留前三后四
// idCard = idCard.replaceAll("(?<=\\w{3})\\w(?=\\w{4})", "*");
}
return idCard;
}

/**
* 手机号码脱敏
*
* @param mobilePhone
* @return
*/
public static String mobilePhoneDesensitization(String mobilePhone) {
// 手机号码保留前三后四
if (StringUtils.isNotEmpty(mobilePhone)) {
mobilePhone = mobilePhone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
}
return mobilePhone;
}

/**
* 电子邮箱脱敏
*
* @param email
* @return
*/
public static String emailDesensitization(String email) {
// 电子邮箱隐藏@前面的3个字符
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;
}

/**
* 银行账号脱敏
*
* @param acctNo
* @return
*/
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;
}

/**
* 客户名称脱敏
*
* @param custName
* @return
*/
public static String custNameDesensitization(String custName) {
// 规则说明:
// 姓名:字符长度小于5位;企业名称:字符长度大于等于5位。
// 姓名规则
// 规则一:1个字则不脱敏,如"张"-->"张"
// 规则二:2个字则脱敏第二个字,如"张三"-->"张*"
// 规则三:3个字则脱敏第二个字,如"张三丰"-->"张*丰"
// 规则四:4个字则脱敏中间两个字,如"易烊千玺"-->"易**玺"
// 企业名称规则:
// 从第4位开始隐藏,最多隐藏6位。

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

/**
* 家庭地址脱敏
*
* @param address
* @return
*/
public static String addressDesensitization(String address) {
// 规则说明:从第4位开始隐藏,隐藏8位。
if (StringUtils.isNotEmpty(address)) {
char[] chars = address.toCharArray();
if (chars.length > 11) {// 由于需要从第4位开始,隐藏8位,因此数据长度必须大于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;

/**
* 姓名脱敏
*
* @param realName
* @return
*/
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);
}
}

/**
* 详细地址脱敏
*
* @param address
* @return
*/
public static String desensitizedAddress(String address){
//江西省宜春市丰城市剑南街道人才教育小区41号丰城住总运营有限公司-->江西省宜春市丰城市剑南街道人才教育小区*************
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;
}

/**
* 对字符串进行脱敏操作
*
* @param origin 原始字符串
* @param prefixNoMaskLen 左侧需要保留几位明文字段
* @param suffixNoMaskLen 右侧需要保留几位明文字段
* @param maskStr 用于遮罩的字符串, 如'*'
* @return 脱敏后结果
*/
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();
}

/**
* 中文姓名,只显示最后一个汉字,其他隐藏为星号,比如:**梦
*
* @param fullName
* @return
*/
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);


}
}