加密解密是信息安全的核心技术,它通过数学算法保护数据在传输和存储过程中的保密性和完整性。
本文系统介绍了加密解密的基本原理和常见算法,是构建安全系统的基础。
脱敏:在不影响数据分析的前提下,对敏感数据进行转换,使其无法反推真实值。
1.2 脱敏规则
class DataMasking:
"""数据脱敏规则库"""
@staticmethod
def mask_phone(phone: str) -> str:
"""
脱敏手机号
13912345678 → 139****5678
"""
if len(phone) < 8:
return phone
return phone[:3] + '*' * (len(phone) - 7) + phone[-4:]
@staticmethod
def mask_email(email: str) -> str:
"""
脱敏邮箱
alice@example.com → a***@example.com
"""
parts = email.split('@')
if len(parts[0]) <= 1:
return parts[0] + '@' + parts[1]
return parts[0][0] + '*' * (len(parts[0]) - 1) + '@' + parts[1]
@staticmethod
def mask_id_card(id_card: str) -> str:
"""
脱敏身份证
110101199001011234 → 1101****9001****1234
"""
if len(id_card) < 8:
return id_card
return id_card[:4] + '*' * (len(id_card) - 8) + id_card[-4:]
@staticmethod
def mask_bank_card(card: str) -> str:
"""
脱敏银行卡
6226092036151234 → 622609****151234
"""
if len(card) < 8:
return card
return card[:6] + '*' * (len(card) - 10) + card[-4:]
@staticmethod
def mask_ip_address(ip: str) -> str:
"""
脱敏IP地址
192.168.1.100 → 192.168.1.*
"""
parts = ip.split('.')
if len(parts) == 4:
return '.'.join(parts[:3]) + '.*'
return ip
@staticmethod
def mask_amount(amount: float, precision: int = 2) -> str:
"""
脱敏金额(用于非精确显示)
12345.67 → 1234* (保留部分数字)
"""
amount_str = str(int(amount))
return amount_str[:-2] + '**' if len(amount_str) > 2 else '***'
2026/6/26大约 5 分钟