当前位置: 首页 > news >正文

敏感数据加密存储实战

敏感数据加密存储实战

一、敏感数据概述

敏感数据是指一旦泄露可能造成安全风险的信息,需要采取加密措施保护。

1.1 敏感数据分类

类别示例加密要求
个人身份身份证号、手机号必须加密
金融信息银行卡号、密码必须加密
业务敏感用户密码、Token必须加密
配置信息密钥、证书必须加密

1.2 加密策略

┌─────────────────────────────────────────────────────────────┐ │ 敏感数据加密流程 │ ├─────────────────────────────────────────────────────────────┤ │ 数据输入 ──▶ 明文 ──▶ 加密 ──▶ 密文 ──▶ 存储 │ │ │ │ │ │ │ ▼ │ │ └──────────────── 解密 ◀───────────┘ │ │ │ │ │ ▼ │ │ 明文输出 │ └─────────────────────────────────────────────────────────────┘

二、加密算法选择

2.1 对称加密

import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import java.util.Base64; public class SymmetricEncryption { private static final String ALGORITHM = "AES"; private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding"; public static String encrypt(String plainText, String key) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.ENCRYPT_MODE, keySpec); byte[] encrypted = cipher.doFinal(plainText.getBytes()); return Base64.getEncoder().encodeToString(encrypted); } public static String decrypt(String cipherText, String key) throws Exception { SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), ALGORITHM); Cipher cipher = Cipher.getInstance(TRANSFORMATION); cipher.init(Cipher.DECRYPT_MODE, keySpec); byte[] decrypted = cipher.doFinal(Base64.getDecoder().decode(cipherText)); return new String(decrypted); } }

2.2 非对称加密

import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import javax.crypto.Cipher; public class AsymmetricEncryption { private static final String ALGORITHM = "RSA"; public static KeyPair generateKeyPair() throws Exception { KeyPairGenerator generator = KeyPairGenerator.getInstance(ALGORITHM); generator.initialize(2048); return generator.generateKeyPair(); } public static byte[] encrypt(byte[] data, PublicKey publicKey) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.ENCRYPT_MODE, publicKey); return cipher.doFinal(data); } public static byte[] decrypt(byte[] data, PrivateKey privateKey) throws Exception { Cipher cipher = Cipher.getInstance(ALGORITHM); cipher.init(Cipher.DECRYPT_MODE, privateKey); return cipher.doFinal(data); } }

2.3 混合加密方案

public class HybridEncryption { public static EncryptedData encrypt(String plainText, PublicKey publicKey) throws Exception { // 生成对称密钥 String symmetricKey = generateRandomKey(16); // 使用对称密钥加密数据 String encryptedData = SymmetricEncryption.encrypt(plainText, symmetricKey); // 使用公钥加密对称密钥 byte[] encryptedKey = AsymmetricEncryption.encrypt(symmetricKey.getBytes(), publicKey); return new EncryptedData(encryptedData, encryptedKey); } public static String decrypt(EncryptedData encryptedData, PrivateKey privateKey) throws Exception { // 使用私钥解密对称密钥 byte[] symmetricKey = AsymmetricEncryption.decrypt(encryptedData.getEncryptedKey(), privateKey); // 使用对称密钥解密数据 return SymmetricEncryption.decrypt(encryptedData.getEncryptedData(), new String(symmetricKey)); } }

三、数据库加密

3.1 字段级加密

@Entity public class User { @Id private Long id; @Column(name = "username") private String username; @Column(name = "password") @Convert(converter = EncryptedStringConverter.class) private String password; @Column(name = "email") @Convert(converter = EncryptedStringConverter.class) private String email; } @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> { @Override public String convertToDatabaseColumn(String attribute) { if (attribute == null) return null; try { return SymmetricEncryption.encrypt(attribute, getSecretKey()); } catch (Exception e) { throw new RuntimeException("Encryption failed", e); } } @Override public String convertToEntityAttribute(String dbData) { if (dbData == null) return null; try { return SymmetricEncryption.decrypt(dbData, getSecretKey()); } catch (Exception e) { throw new RuntimeException("Decryption failed", e); } } }

3.2 数据库透明加密

# MySQL TDE配置 innodb_encrypt_tables = ON innodb_encryption_threads = 4 innodb_encrypt_log = ON innodb_encryption_rotate_key_age = 1

3.3 密码哈希

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; public class PasswordUtil { private static final BCryptPasswordEncoder encoder = new BCryptPasswordEncoder(); public static String hashPassword(String rawPassword) { return encoder.encode(rawPassword); } public static boolean verifyPassword(String rawPassword, String encodedPassword) { return encoder.matches(rawPassword, encodedPassword); } }

四、密钥管理

4.1 密钥存储

@Configuration public class KeyManagementConfig { @Bean public String secretKey() { // 从密钥管理服务获取密钥 return keyManagementService.getSecret("app-encryption-key"); } }

4.2 密钥轮换

public class KeyRotationService { public void rotateKey() { // 生成新密钥 String newKey = generateRandomKey(32); // 更新密钥存储 keyManagementService.updateSecret("app-encryption-key", newKey); // 重新加密所有数据(可选) reEncryptAllData(newKey); } }

4.3 使用密钥管理服务

public class CloudKeyManager { private final AWSKMS kmsClient; public byte[] encryptWithKms(byte[] data) { EncryptRequest request = EncryptRequest.builder() .keyId("arn:aws:kms:us-east-1:123456789012:key/1234abcd-12ab-34cd-56ef-1234567890ab") .plaintext(data) .build(); return kmsClient.encrypt(request).ciphertextBlob().array(); } }

五、安全传输

5.1 HTTPS配置

server: port: 443 ssl: enabled: true key-store: classpath:keystore.p12 key-store-password: ${KEY_STORE_PASSWORD} key-store-type: PKCS12 key-alias: myapp

5.2 TLS证书管理

# 使用Let's Encrypt获取证书 certbot certonly --webroot -w /var/www/html -d example.com # 自动续期 certbot renew --dry-run

六、文件加密

6.1 文件加密工具

public class FileEncryption { public static void encryptFile(String inputPath, String outputPath, String key) throws Exception { try (FileInputStream fis = new FileInputStream(inputPath); FileOutputStream fos = new FileOutputStream(outputPath); CipherOutputStream cos = new CipherOutputStream(fos, getCipher(Cipher.ENCRYPT_MODE, key))) { byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = fis.read(buffer)) != -1) { cos.write(buffer, 0, bytesRead); } } } public static void decryptFile(String inputPath, String outputPath, String key) throws Exception { try (FileInputStream fis = new FileInputStream(inputPath); CipherInputStream cis = new CipherInputStream(fis, getCipher(Cipher.DECRYPT_MODE, key)); FileOutputStream fos = new FileOutputStream(outputPath)) { byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = cis.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); } } } }

七、脱敏处理

7.1 数据脱敏

public class DataMaskingUtil { public static String maskPhone(String phone) { if (phone == null || phone.length() < 11) return phone; return phone.substring(0, 3) + "****" + phone.substring(7); } public static String maskIdCard(String idCard) { if (idCard == null || idCard.length() < 18) return idCard; return idCard.substring(0, 4) + "**********" + idCard.substring(14); } public static String maskEmail(String email) { if (email == null || !email.contains("@")) return email; String[] parts = email.split("@"); if (parts[0].length() <= 2) return email; return parts[0].substring(0, 2) + "****@" + parts[1]; } }

7.2 日志脱敏

@Aspect public class LogMaskingAspect { @Around("execution(* com.example..*.*(..))") public Object maskLog(ProceedingJoinPoint joinPoint) throws Throwable { Object result = joinPoint.proceed(); // 对日志输出进行脱敏处理 maskSensitiveData(result); return result; } }

八、安全审计

8.1 访问日志

@Configuration public class AuditLogConfig { @Bean public Filter auditFilter() { return (request, response, chain) -> { HttpServletRequest httpRequest = (HttpServletRequest) request; AuditLog log = AuditLog.builder() .userId(getUserId(httpRequest)) .action(httpRequest.getRequestURI()) .method(httpRequest.getMethod()) .ip(getClientIp(httpRequest)) .timestamp(LocalDateTime.now()) .build(); auditLogRepository.save(log); chain.doFilter(request, response); }; } }

8.2 数据变更审计

@EntityListeners(AuditingEntityListener.class) @MappedSuperclass public abstract class Auditable { @CreatedBy @Column(name = "created_by") private String createdBy; @CreatedDate @Column(name = "created_date") private LocalDateTime createdDate; @LastModifiedBy @Column(name = "last_modified_by") private String lastModifiedBy; @LastModifiedDate @Column(name = "last_modified_date") private LocalDateTime lastModifiedDate; }

九、合规要求

9.1 GDPR合规

public class GdprService { public void rightToBeForgotten(Long userId) { // 删除用户所有数据 userRepository.deleteById(userId); // 删除相关日志 auditLogRepository.deleteByUserId(userId); // 删除缓存数据 redisTemplate.delete("user:" + userId); } public void dataPortability(Long userId) { // 导出用户所有数据 UserData data = exportUserData(userId); // 提供下载 sendDataExport(data); } }

9.2 数据分类分级

级别描述保护措施
L1公开数据无需加密
L2内部数据访问控制
L3敏感数据加密存储
L4机密数据加密+访问审计

十、总结

敏感数据加密存储需要综合考虑:

  1. 选择合适算法:对称加密用于数据,非对称加密用于密钥
  2. 密钥安全管理:使用专业密钥管理服务
  3. 多层防护:传输加密、存储加密、访问控制
  4. 合规审计:满足GDPR等法规要求
  5. 定期轮换:定期更换密钥,降低泄露风险

通过系统化的加密策略,可以有效保护敏感数据的安全。

http://www.jsqmd.com/news/874213/

相关文章:

  • 通过 TaoToken 用量分析功能优化模型选型与调用策略
  • SLAM技术路线收敛?不,多模态融合正在重启路线之争
  • 前缀和与差分进阶总结 | 技巧归纳与实战应用
  • Go语言CI/CD流水线实践
  • 【GO context 】上下文取消/超时的本质
  • 无语,Trae的AI编程想混过去啊,我就说了点重话:我只要结果,我需要一个成语接龙程序,这个程序能正确运行,可以通过验收!
  • 2026第三方配送平台选型指南:成都本地跑腿加盟/成都本地配送平台/成都第三方配送平台/成都聚合配送平台/成都自配送平台/选择指南 - 优质品牌商家
  • 2026泳池设计优质厂家推荐:泳池设计/洗浴厂家/洗浴工程/洗浴改造/洗浴施工/洗浴设备/温泉洗浴设计/游泳池改造/选择指南 - 优质品牌商家
  • 企业级条码处理方案:ZXing.Net在.NET生态中的架构实践与性能优化
  • 【Appium 系列】第18节-重试与容错 — 移动端测试的稳定性保障
  • 2026泳池建造厂家推荐:酒店洗浴、户外泳池、泳池工程、泳池水处理、泳池设备、洗浴厂家、洗浴工程、洗浴改造、洗浴施工选择指南 - 优质品牌商家
  • 锌钢护栏网技术解析:四川公路铁路护栏网、四川双边丝护栏网、四川围栏网、四川学校球场围栏、四川市政道路护栏网、四川牛栏围栏网选择指南 - 优质品牌商家
  • 2026年Q2四川应急物资厂家评测:应急消防设备厂家/应急物资厂家电话/抗洪抢险应急设备/消防工具厂家/消防智能设备/选择指南 - 优质品牌商家
  • 2026成都靠谱金属建材回收公司推荐:工厂废料回收/工地废料回收/库房物资回收/废旧机器回收/废铁回收/废铜回收/选择指南 - 优质品牌商家
  • 毕业论文神器!2026年必备AI论文软件榜单,免费版也能写合规初稿
  • 2026年Q2西南地区测绘仪租赁服务机构排行盘点:华测rtk/华测无人船/地形测量/大疆无人机/徕卡全站仪/手持扫描仪/选择指南 - 优质品牌商家
  • 2026年当下河北工程网格布实力厂商剖析与精准选型指南 - 2026年企业推荐榜
  • 2026年成都学历提升选校指南:口碑机构成都市成华区新概念外语培训学校深度 - 2026年企业推荐榜
  • 2026年当下耐磨输送带选型指南:鼎基机械输送有限公司深度解析 - 2026年企业推荐榜
  • 2026年5月,如何精准对接武汉地区优质橡胶助剂供应商? - 2026年企业推荐榜
  • 2026年第二季度,昆明膜结构源头工厂如何引领市场新需求 - 2026年企业推荐榜
  • 【独家首发】Claude代码生成能力黄金分级标准(L1-L5):附赠可落地的团队接入评估清单(限前500名下载)
  • AI知识管理不是工具升级,而是教学主权重构:一位特级教师用18个月完成“教案→知识流→认知干预”三级跃迁(全程数据脱敏实录)
  • Claude+Query Store双引擎协同优化(仅限AWS RDS与Azure SQL托管实例的私有API调用指南)
  • 合同纠纷律师哪个好?李静律师:复杂商事合同争议解决专家 - 外贸老黄
  • 当Agent开始质疑你的原始数据——AI驱动的数据质量自治体系构建(含动态污点追踪与因果溯源模块)
  • 2026气体扩散层权威供应商精选推荐:气体扩散过滤板、气体扩散金属板、气体扩散钛板、气体扩散钛滤板、电解槽滤板选择指南 - 优质品牌商家
  • 2026防爆门厂家推荐:快速门推荐/折叠门厂家/折叠门推荐/推拉门厂家/推拉门推荐/提升门推荐/泄爆窗厂家/泄爆门厂家/选择指南 - 优质品牌商家
  • 3层深度清理技术:Display Driver Uninstaller显卡驱动彻底卸载解决方案
  • 2026安防行业监控操作台厂家选购推荐:落地式机柜/一体化机柜/不锈钢操作台厂家/冷通道机柜/四川机柜厂家推荐/选择指南 - 优质品牌商家