管理系统必备技(5):关于手机号脱敏及加密的开发记录

2,732 阅读2分钟

一、背景

网络安全等级保护中,判定的规则是:存储在数据库的姓名+手机号算敏感信息,单纯的手机号不算,而敏感信息需要加密。

二、开发记录

2.1 思路

首先,对于敏感信息,手机号需要做加密。而加密的方式需要可以能够回显,因此,MD5是可以排除了。而之前对密码采用SM2非对称加密,这个把公钥私钥保存到数据库中,显然比较麻烦,因此,在这里可以使用对称加密来实现,采用AES对称加密实现存储。然后返回给前端的时候需要对手机号进行脱敏,也就是对中间四位替换成*号。

2.2 AES对称加密

public class AESUtil {
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";

    // appKey,每隔一段时间进行替换即可
    // 可以设计成保存到数据库中或者那里,然后进行每隔一段时间进行替换,增加保密的安全性
    public static   final String appKey = "fa8f92af-1123-d2rfd-9626-xl2b64481320";
    public static String encrypt(String content, String appKey) {
        try {
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);// 创建密码器
            byte[] byteContent = content.getBytes("utf-8");
            cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(appKey));// 初始化为加密模式的密码器
            byte[] result = cipher.doFinal(byteContent);// 加密
            return Base64.encodeBase64String(result);// 通过Base64转码返回
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static String decrypt(String content, String appKey) {
        try {
            // 实例化
            Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
            // 使用密钥初始化,设置为解密模式
            cipher.init(Cipher.DECRYPT_MODE, getSecretKey(appKey));
            // 执行操作
            byte[] result = cipher.doFinal(Base64.decodeBase64(content));
            return new String(result, "utf-8");
        } catch (Exception ex) {
        }
        return "";
    }

    private static SecretKeySpec getSecretKey(String appKey) {
        // 返回生成指定算法密钥生成器的 KeyGenerator 对象
        KeyGenerator kg = null;
        try {
            kg = KeyGenerator.getInstance(KEY_ALGORITHM);
            // SecureRandom 实现随操作系统本身的內部状态,除非调用方在调用 getInstance 方法之后又调用了 setSeed 方法;该实现在
            // windows 上每次生成的 key 都相同,但是在 solaris 或部分 linux 系统上则不同。解决在linux操作系统中加密产生的字符串不一致问题。
            SecureRandom secureRandom = SecureRandom.getInstance("SHA1PRNG");
            secureRandom.setSeed(appKey.getBytes());
            // AES 要求密钥长度为 128
            kg.init(128, secureRandom);
            // 生成一个密钥
            SecretKey secretKey = kg.generateKey();
            return new SecretKeySpec(secretKey.getEncoded(), KEY_ALGORITHM);// 转换为AES专用密钥
        } catch (NoSuchAlgorithmException ex) {
            ex.printStackTrace();
        }
        return null;
    }
}

加密解密判断:

加密
String encryptUserNo = AESUtil.encrypt(userNo, AESUtil.appKey);
解密
String decryptUserNo = AESUtil.decrypt(encryptUserNo, AESUtil.appKey);

2.3 手机号脱敏

 String phone =AESUtil.decrypt(i.getPhone(), AESUtil.appKey);
      if(ObjectUtil.isNotEmpty(phone))
      {
      i.setPhone(new StringBuilder(phone).replace(3,7,"****").toString());
      }

2.4 前后端手机号校验

后端:

public static final String phone2 = "^[1][3-8][0-9]{9}$";
 
public static boolean checkPhone(String phone){

        Pattern pattern = Pattern.compile(phone2);
        Matcher matcher = pattern.matcher(phone);
        if (matcher.matches()) {
            return true;
        }
        return false;
    }

前端:

var phoneReg = new RegExp("^[1][3-8][0-9]{9}$");

 else if(!phoneReg.test(user.phone)) {
        this.$message.error('手机号格式不正确')
      }

2.5 展示结果

image.png

image.png