SoFunction
Updated on 2025-04-09

Java's operation guide for implementing data desensitization (Desensitization)

1. Brief description

Desensitization refers to protecting the security of sensitive information during storage and use by partially or completely hiding sensitive data. Common application scenarios include logging, interface return, report display, data analysis, etc., especially in systems involving user privacy, payment information and enterprise data.

2. The significance of data desensitization

  • Protect privacy
    Prevent sensitive data leakage, such as ID number, bank card number, mobile phone number, etc.

  • Comply with compliance requirements
    Comply with regulations such as the Personal Information Protection Law (PIPL) and the General Data Protection Regulations (GDPR).

  • Reduce data risks
    Even if the data is leaked during transmission and use, sensitive information will not be exposed.

Common desensitization scenarios:

  • Desensitization of mobile phone number
    Example: 13912345678 -> 139****5678

  • Identification number
    Example: 123456789012345678 -> 123456******5678

  • Email desensitization
    Example: example@ -> ex****@

  • Desensitization of bank card number
    Example: 6222020400112568888 -> 6222 **** **** 8888

3. Examples of data desensitization

3.1 Custom desensitization implementation

Add the desensitization tool class DesensitizationUtils:

public class DesensitizationUtils {

    // Desensitization of mobile phone number    public static String maskPhone(String phone) {
        if (phone == null || () != 11) {
            return phone;
        }
        return (0, 3) + "****" + (7);
    }

    // Identity card number desensitized    public static String maskIdCard(String idCard) {
        if (idCard == null || () < 15) {
            return idCard;
        }
        return (0, 6) + "********" + (() - 4);
    }

    // Email desensitization    public static String maskEmail(String email) {
        if (email == null || !("@")) {
            return email;
        }
        int atIndex = ("@");
        if (atIndex <= 1) {
            return email;
        }
        return (0, 2) + "****" + (atIndex);
    }

    // Desensitization of bank card number    public static String maskBankCard(String bankCard) {
        if (bankCard == null || () < 16) {
            return bankCard;
        }
        return (0, 4) + " **** **** " + (() - 4);
    }
}

Examples of desensitization:

public class Main {
    public static void main(String[] args) {
        String phone = "13912345678";
        String idCard = "123456789012345678";
        String email = "example@";
        String bankCard = "6222020400112568888";

        ("Desensitized cell phone number: " + (phone));
        ("Desensitized ID number: " + (idCard));
        ("Desensitized Email: " + (email));
        ("Desensitized bank card number: " + (bankCard));
    }
}

3.1 Reference Component desensitization implementation

The following is a complete solution to implement data desensitization using the Maven package provided, including Maven dependencies and implementation steps.

It is a library dedicated to data desensitization, supporting a variety of desensitization strategies. The following are the related dependencies:

<!-- data desensitization-->
<dependency>
    <groupId></groupId>
    <artifactId>desensitization</artifactId>
    <version>2.4.3</version>
</dependency>

An annotation-based desensitization mechanism is provided, first adding annotations to the fields that need to be desensitized:

package ;

import .*;

public class User {

    @ChineseNameSensitive
    private String name;

    @IdCardNumberSensitive
    private String idCard;

    @PhoneNumberSensitive
    private String phone;

    @EmailSensitive
    private String email;

    @PasswordSensitive
    private String password;

    // Getters and Setters
    public String getName() {
        return name;
    }

    public void setName(String name) {
         = name;
    }

    public String getIdCard() {
        return idCard;
    }

    public void setIdCard(String idCard) {
         = idCard;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
         = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
         = email;
    }

    public  String getPassword() {
        return password;
    }

    public void setPassword(String password) {
         = password;
    }
}

Use the Sensitive tool class in the library to desensitize:

public class DesensitizationController {
    public static void main(String[] args) {
        User user = new User();
        ("admin@");
        ("89144552522");
        ("admin");
        ("13845988146");
        ("6222020400112568888");

        user = (user);

        ("Desensitized User Information: " + (user));
    }
}

Output example:

User information after desensitization: {"email":"a****@","idCard":"622202*********8888","name":"a****","password":"***********","phone":"138****8146"}

It provides flexible desensitization based on annotation and processor, supports sensitive data protection in multiple scenarios, and is especially suitable for Java projects that require dynamic desensitization.

4. Summary

Data desensitization is a key technology for protecting sensitive information. It can not only achieve desensitization in common scenarios through simple tools, but also use annotation and reflection to achieve flexible automatic desensitization. In actual development, it is recommended to choose an appropriate desensitization solution based on business needs and performance requirements to ensure the security of sensitive information.

Application scenarios:

  • User privacy display: the user's mobile phone number or name in the order information.
  • Logging: Hide user-sensitive data when recording error messages.
  • Data analysis: Avoid leakage of sensitive information when exporting or sharing data.

Through a reasonable desensitization strategy, the risk of data breaches can be effectively reduced while meeting the legal requirements of privacy protection.

This is the article about Java's operation guide for implementing data desensitization. For more related Java data desensitization, please search for my previous articles or continue browsing the following related articles. I hope everyone will support me in the future!