/**
*
* 隐秘数据工具类
*
* @version 1.0
* @since JDK1.7
* @author yaomy
* @date 2018年4月3日 上午10:19:07
*/
public class HideDataUtil {
/**
* @param code 要隐藏显示的字符串
* @param head 前面保留的位数
* @param tail 后面保留的位数
* @return 处理后的字符串
*/
public static String getEncryptCode(String code,int head,int tail){
// 中间要隐藏的位数
int body = code.length() - head - tail;
// 将字符串拆成三部分,并指定每部分长度的正则表达式
String regexVar = "(\\w{%d})(\\w{%d})(\\w{%d})";
String regex = String.format(regexVar, head, body, tail);
// 获取字符串中间要隐藏的部分,并替换成对应长度的*
String bodyPart = code.replaceAll(regex, "$2");
System.out.println(bodyPart);
String bodyEncrypt = bodyPart.replaceAll("\\w", "*");
// 处理生成字符串replacement = "$1*****$3" 中间是对应长度的*号
String replacement = String.format("$1%s$3", bodyEncrypt);
return code.replaceAll(regex, replacement);
}
}
数据隐藏工具类
最新推荐文章于 2022-11-28 14:22:46 发布