Skip to content

隐私数据保护

鸿蒙隐私保护:敏感数据加密、权限最小化、用户知情同意。


1. 隐私数据分类

1.1 敏感数据类型

| 类型 | 说明 | 保护级别 | |---|-|-|-| | 个人身份信息 | 姓名、身份证、手机号 | 高 | | 生物特征 | 指纹、人脸、声纹 | 高 | | 位置信息 | GPS 定位、轨迹 | 高 | | 通信记录 | 通话、短信、聊天记录 | 高 | | 健康数据 | 心率、血压、运动数据 | 高 | | 财务信息 | 银行卡、支付记录 | 高 | | 设备信息 | IMEI、MAC 地址 | 中 | | 使用习惯 | 浏览记录、搜索历史 | 中 |


2. 隐私数据保护机制

2.1 数据加密存储

typescript
import { crypto } from '@kit.CryptoArchitectureKit'
import { preferences } from '@kit.ArkUtils'

class PrivacyDataManager {
    private store: preferences.Preferences | null = null
    private encryptKey: crypto.CryptoKey | null = null

    async init() {
        // 初始化加密密钥
        this.encryptKey = crypto.generateKey({
            algorithm: crypto.Algorithm.AES_256_GCM,
            keySize: 256
        })

        // 获取存储
        let ctx = context.getApplicationContext()
        this.store = await preferences.getPreferences(ctx, { name: 'privacy' })
    }

    // 加密存储敏感数据
    async saveSensitiveData(key: string, value: string): Promise<void> {
        if (!this.store || !this.encryptKey) return

        // AES 加密
        let encrypted = crypto.encrypt({
            key: this.encryptKey,
            data: new TextEncoder().encode(value),
            algorithm: crypto.Algorithm.AES_256_GCM
        })

        // 存储加密数据
        await this.store.put(`_${key}`, encrypted)
        await this.store.commit()
    }

    // 解密读取敏感数据
    async getSensitiveData(key: string): Promise<string> {
        if (!this.store || !this.encryptKey) return ''

        let encrypted = await this.store.get<Uint8Array>(`_${key}`)
        if (!encrypted) return ''

        // AES 解密
        let decrypted = crypto.decrypt({
            key: this.encryptKey,
            data: encrypted,
            algorithm: crypto.Algorithm.AES_256_GCM
        })

        return new TextDecoder().decode(decrypted)
    }
}

3. 权限最小化

3.1 最小权限原则

✅ 推荐:
├─ 只申请必要的权限
├─ 按需申请(用时再申请)
├─ 权限用完及时释放
└─ 提供无权限的降级方案

❌ 避免:
├─ 一次性申请所有权限
├─ 与应用功能无关的权限
├─ 过度收集用户数据
└─ 后台持续使用敏感权限

3.2 权限使用场景说明

json5
// module.json5
"requestPermissions": [
    {
        "name": "ohos.permission.CAMERA",
        "reason": "$string:camera_reason",  // 向用户说明为什么需要
        "usedScene": {
            "abilities": ["EntryAbility"],
            "when": "inuse"  // 仅使用时授权
        }
    }
]
typescript
// string.json
{
    "camera_reason": "用于拍摄照片和扫描二维码,实现头像上传和快捷登录功能"
}

4. 用户知情同意

4.1 隐私政策弹窗

typescript
@Component
struct PrivacyPolicyDialog {
    @State showDialog: boolean = true
    @State agreed: boolean = false

    build() {
        if (this.showDialog) {
            AlertDialog({
                title: '隐私政策',
                message: '我们尊重并保护您的隐私...\n\n' +
                         '我们需要以下权限:\n' +
                         '• 相机:用于扫描二维码\n' +
                         '• 位置:用于附近的人功能\n' +
                         '• 存储:用于保存照片\n\n' +
                         '请阅读完整隐私政策...',
                primaryButton: {
                    value: '同意',
                    action: () => {
                        this.agreed = true
                        this.showDialog = false
                        this.saveAgreement()
                    }
                },
                secondaryButton: {
                    value: '不同意',
                    action: () => {
                        this.showDialog = false
                        // 退出应用或限制功能
                    }
                }
            })
        }
    }

    async saveAgreement() {
        let store = await preferences.getPreferences(context, { name: 'privacy' })
        await store.put('policyAgreed', true)
        await store.put('agreedTime', Date.now())
        await store.commit()
    }
}

5. 面试高频考点

Q1: 隐私数据如何保护?

回答:敏感数据 AES 加密存储(AssetStore/Preferences)、权限最小化、用户知情同意、用完及时释放。

Q2: 权限最小化原则?

回答:只申请必要权限,按需申请,提供降级方案,不强制用户授权无关权限。


🐱 小猫提示:隐私保护记住 "加密存储、最小权限、知情同意、用完释放"