这里写自定义目录标题
Java项目中对手机号加密脱敏
项目中,可能需要对手机号进行加密,接下来就来介绍一下如何实现。
思路:首先要封装其手机号
set方法对其手机号的位数进行限制。
get方法用substring方法对手机号进行加密。
1. 代码:
private String phone;
public String getPhone() {
if(phone!=null){
return phone.substring(0,3)+"****"+phone.substring(7,11);
}
return " ";
}
public void setPhone(String phone) {
if(phone.length()!=11){
System.out.println("手机号输入错误!");
}else{
this.phone = phone;
}
}
2. 输出
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
shoujihjao d1 = new shoujihjao();
System.out.println("请输入您的手机号");
d1.setPhone(sc.nextLine());
System.out.println("进行加密中...");
System.out.println(d1.getPhone());
}
3. 结果
结果:请输入您的手机号
18700000000
进行加密中...
187****0000