java数据打码/脱敏工具类(姓名,手机号码,身份证,详细地址)
2021/7/27 20:08:15
本文主要是介绍java数据打码/脱敏工具类(姓名,手机号码,身份证,详细地址),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.tms.common.utils; import com.baomidou.mybatisplus.toolkit.StringUtils; public class EncryptionUtils { /** * 定义所有常量 */ public static final int ONE = 1; public static final int TWO = 2; /* * 姓名 * */ public static String desensitizedName(String str) { if (str == null) { return null; } if (str.length() == ONE) { return str; } else if (str.length() == TWO) { return str.substring(0, 1) +"*"; } else { Integer length = str.length(); StringBuffer middle = new StringBuffer(); for (int i = 0; i < str.substring(1, length - 1).length(); i++) { middle.append("*"); } return str.substring(0, 1) + middle + str.substring(length - 1, length); } } /* * 手机号码 * */ public static String desensitizedPhoneNumber(String phoneNumber){ if(StringUtils.isNotEmpty(phoneNumber)){ phoneNumber = phoneNumber.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2"); } return phoneNumber; } /* * 身份证 * */ public static String desensitizedIdNumber(String idNumber){ if (StringUtils.isNotEmpty(idNumber)) { if (idNumber.length() == 15){ idNumber = idNumber.replaceAll("(\\w{6})\\w*(\\w{3})", "$1******$2"); } if (idNumber.length() == 18){ idNumber = idNumber.replaceAll("(\\w{6})\\w*(\\w{3})", "$1*********$2"); } } return idNumber; } /* * 详细地址 * */ 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 void main(String[] args) { System.out.println("名字: " + EncryptionUtils.desensitizedName("张子枫")); System.out.println("名字: " + EncryptionUtils.desensitizedName("易烊千玺")); System.out.println("名字: " + EncryptionUtils.desensitizedName("玛克西姆·高尔基")); System.out.println("名字: " + EncryptionUtils.desensitizedName("古力娜扎尔·拜合提亚尔")); System.out.println("=============================================="); System.out.println("手机号: " + desensitizedPhoneNumber("13100355465")); System.out.println("手机号: " + desensitizedPhoneNumber("15616966574")); System.out.println("手机号: " + desensitizedPhoneNumber("15921996220")); System.out.println("手机号: " + desensitizedPhoneNumber("111111234112")); System.out.println("手机号: " + desensitizedPhoneNumber("1310038123412345465")); System.out.println("=============================================="); System.out.println("身份证: " + desensitizedIdNumber("51130219860910074116")); System.out.println("身份证: " + desensitizedIdNumber("44058219980704544X")); System.out.println("身份证: " + desensitizedIdNumber("510681199904111823")); System.out.println("身份证: " + desensitizedIdNumber("341223199005111329")); System.out.println("身份证: " + desensitizedIdNumber("211382198312023415")); System.out.println("身份证: " + desensitizedIdNumber("230103196310100225")); System.out.println("身份证: " + desensitizedIdNumber("431081200001241189")); System.out.println("=============================================="); System.out.println("地址: " + desensitizedAddress("四川省遂宁船山新桥镇新桥火车站")); System.out.println("地址: " + desensitizedAddress("四川省成都市锦江区柳江街道三色路华润东原时光绘三栋1817")); System.out.println("地址: " + desensitizedAddress("广东省广州市番禺区大石街道大石镇礼村中路南约街8号")); System.out.println("地址: " + desensitizedAddress("浙江省温州市瑞安市安阳街道中瑞景苑8栋")); System.out.println("地址: " + desensitizedAddress("安徽省合肥市庐阳区双岗街道阜阳路桥西沿河路中央花园小区4栋4单元1507")); System.out.println("地址: " + desensitizedAddress("江西省宜春市丰城市剑南街道人才教育小区41号丰城住总运营有限公司")); System.out.println("地址: " + desensitizedAddress("上海上海市浦东新区北蔡镇御桥路1978弄38号501")); System.out.println("=============================================="); } }
结果如下:
这篇关于java数据打码/脱敏工具类(姓名,手机号码,身份证,详细地址)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-24怎么修改Kafka的JVM参数?-icode9专业技术文章分享
- 2024-12-23线下车企门店如何实现线上线下融合?
- 2024-12-23鸿蒙Next ArkTS编程规范总结
- 2024-12-23物流团队冬至高效运转,哪款办公软件可助力风险评估?
- 2024-12-23优化库存,提升效率:医药企业如何借助看板软件实现仓库智能化
- 2024-12-23项目管理零负担!轻量化看板工具如何助力团队协作
- 2024-12-23电商活动复盘,为何是团队成长的核心环节?
- 2024-12-23鸿蒙Next ArkTS高性能编程实战
- 2024-12-23数据驱动:电商复盘从基础到进阶!
- 2024-12-23从数据到客户:跨境电商如何通过销售跟踪工具提升营销精准度?