When to Use
- - Encrypting files, database fields, or app storage
- Password hashing (bcrypt, argon2)
- Key management, rotation, derivation
- TLS/certificate configuration
- Auditing code for crypto mistakes
- Mobile secure storage (Keychain, Keystore)
Algorithm Selection
| Purpose | Use | Avoid |
|---|
| Passwords | argon2id, bcrypt (cost≥12) | MD5, SHA1, plain SHA256 |
| Symmetric |
AES-256-GCM, ChaCha20-Poly1305 | AES-ECB, DES, RC4 |
| Asymmetric | RSA-4096+OAEP, Ed25519, P-256 | RSA-1024, PKCS#1 v1.5 |
| Key derivation | PBKDF2 (≥600k), scrypt, argon2 | Single-pass hash |
| JWT signing | RS256, ES256 | HS256 with weak secret |
| TLS | 1.2+ only | TLS 1.0/1.1, SSLv3 |
Critical Rules
- 1. Never reuse IVs/nonces — AES-GCM + repeated nonce = catastrophic
- Use authenticated encryption (AEAD) — Plain CBC enables padding oracles
- Hash passwords, don't encrypt — Hashing is one-way
- No hardcoded keys — Use env vars, KMS, or Vault
- No Math.random() for crypto — Use CSPRNG only
- Constant-time comparisons — Prevent timing attacks on secrets
- Separate keys by purpose — Encryption ≠ signing ≠ backup
File Encryption (CLI)
CODEBLOCK0
Platform-Specific
See patterns.md for code snippets:
- - Password hashing (Node, Python, Go)
- Envelope encryption with KMS
- JWT with RS256 key rotation
- Secure token generation
See mobile.md for:
- - iOS Keychain wrapper
- Android EncryptedSharedPreferences
- SQLCipher setup
- Biometric auth integration
- Certificate pinning
See infra.md for:
- - TLS certificate auto-renewal
- HashiCorp Vault policies
- mTLS between services
- Backup encryption verification
Audit Checklist
- - [ ] No plaintext passwords in DB/logs/env
- [ ] No secrets in git history
- [ ] No hardcoded keys in source
- [ ] No Math.random() for security
- [ ] No deprecated algorithms (MD5, SHA1, DES)
- [ ] No disabled cert validation
- [ ] IVs/nonces never reused
- [ ] PBKDF2 iterations ≥600k / bcrypt cost ≥12
- [ ] TLS 1.2+ enforced, old protocols disabled
- [ ] Key rotation procedure documented
技能名称: 加密
详细描述:
使用场景
- - 加密文件、数据库字段或应用存储
- 密码哈希(bcrypt、argon2)
- 密钥管理、轮换、派生
- TLS/证书配置
- 审计代码中的加密错误
- 移动端安全存储(Keychain、Keystore)
算法选择
| 用途 | 推荐使用 | 避免使用 |
|---|
| 密码 | argon2id、bcrypt(成本≥12) | MD5、SHA1、纯SHA256 |
| 对称加密 |
AES-256-GCM、ChaCha20-Poly1305 | AES-ECB、DES、RC4 |
| 非对称加密 | RSA-4096+OAEP、Ed25519、P-256 | RSA-1024、PKCS#1 v1.5 |
| 密钥派生 | PBKDF2(≥600k)、scrypt、argon2 | 单次哈希 |
| JWT签名 | RS256、ES256 | 弱密钥的HS256 |
| TLS | 仅1.2及以上版本 | TLS 1.0/1.1、SSLv3 |
关键规则
- 1. 切勿重复使用IV/随机数 — AES-GCM + 重复随机数 = 灾难性后果
- 使用认证加密(AEAD) — 纯CBC模式会导致填充预言攻击
- 密码应哈希而非加密 — 哈希是单向的
- 禁止硬编码密钥 — 使用环境变量、KMS或Vault
- 加密场景禁止使用Math.random() — 仅使用密码学安全伪随机数生成器
- 使用常量时间比较 — 防止针对秘密的时序攻击
- 按用途分离密钥 — 加密密钥 ≠ 签名密钥 ≠ 备份密钥
文件加密(命令行)
bash
age(现代、简单)
age -p -o file.age file.txt
age -d -o file.txt file.age
GPG
gpg -c --cipher-algo AES256 file.txt
平台特定
参见 patterns.md 获取代码示例:
- - 密码哈希(Node、Python、Go)
- 使用KMS的信封加密
- 带RS256密钥轮换的JWT
- 安全令牌生成
参见 mobile.md 获取:
- - iOS Keychain封装
- Android EncryptedSharedPreferences
- SQLCipher配置
- 生物识别认证集成
- 证书固定
参见 infra.md 获取:
- - TLS证书自动续期
- HashiCorp Vault策略
- 服务间mTLS
- 备份加密验证
审计清单
- - [ ] 数据库/日志/环境中无明文密码
- [ ] Git历史中无秘密信息
- [ ] 源码中无硬编码密钥
- [ ] 安全场景未使用Math.random()
- [ ] 未使用已弃用算法(MD5、SHA1、DES)
- [ ] 未禁用证书验证
- [ ] IV/随机数从未重复使用
- [ ] PBKDF2迭代次数≥600k / bcrypt成本≥12
- [ ] 强制使用TLS 1.2+,禁用旧协议
- [ ] 密钥轮换流程已文档化