How to implement data desensitization in java and implement data desensitization in java
What does data desensitization mean?
Data desensitization refers to the deformation of sensitive information by masking rules to ensure reliable protection of sensitive and private data. If the customer's security data or some commercial sensitive data is involved, the real data will be transformed and used for testing without violating system rules, for example, personal information such as ID card number, mobile phone number, card number, and customer number must be desensitized. This is one of the database security technologies.
For example, we now have a user table that contains fields such as name, gender, email address, and phone number. However, when we view the data, we do not want the data to be exposed, at this time, the data can be desensitized. Of course, you can select which fields need to be desensitized and then output. As for how to choose, it is another thing.
As shown in the following table, the data is desensitized and encrypted: (all fields are desensitized)
The following is code implementation:
private static final int SIZE = 6; private static final String SYMBOL = "*"; public static String toConceal(String value) { if (null == value || "".equals(value)) { return value; } int len = value.length(); int pamaone = len / 2; int pamatwo = pamaone - 1; int pamathree = len % 2; StringBuilder stringBuilder = new StringBuilder(); if (len <= 2) { if (pamathree == 1) { return SYMBOL; } stringBuilder.append(SYMBOL); stringBuilder.append(value.charAt(len - 1)); } else { if (pamatwo <= 0) { stringBuilder.append(value.substring(0, 1)); stringBuilder.append(SYMBOL); stringBuilder.append(value.substring(len - 1, len)); } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) { int pamafive = (len - SIZE) / 2; stringBuilder.append(value.substring(0, pamafive)); for (int i = 0; i < SIZE; i++) { stringBuilder.append(SYMBOL); } if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) { stringBuilder.append(value.substring(len - pamafive, len)); } else { stringBuilder.append(value.substring(len - (pamafive + 1), len)); } } else { int pamafour = len - 2; stringBuilder.append(value.substring(0, 1)); for (int i = 0; i < pamafour; i++) { stringBuilder.append(SYMBOL); } stringBuilder.append(value.substring(len - 1, len)); } } return stringBuilder.toString(); }
This is a common desensitization method and may not meet your specific requirements. Of course, you can desensitize each type of data (such as email, phone number, and ID card number) in a specific way, such as the last four digits after the phone number is hidden, and the ID card number is hidden by year, month, and day, you only need to add some logic.