如何在 Firebase 中加密用户数据 [英] How to encrypt user data in Firebase
问题描述
我正在使用 Firebase 的电子邮件/密码登录方法.我想在将用户保存到实时数据库中的数据发送到数据库之前对其进行加密.Firebase 已经处理了用户密码,但是我可以以某种方式使用它来加密只有客户端无法解密的数据吗?如果我能用客户端 sdk 实现它就好了.
I am using the email/password sign in method for Firebase. I would like to encrypt the data users save into the realtime database before sending it to the database. Firebase already handle the user password, but can I somehow use it to encrypt data which can't be decrypted by me only the client? It would be nice if I could achieve it with the client sdk.
所以我的流程是这样的:
So my flow would be something like this:
- 用户使用其凭据登录(由 firebase 本身处理)
- 用户使用一些唯一的密钥加密一些数据,这些密钥只能从凭据或一些仅对用户可用的数据中生成,而我不能.(此密钥需要在会话之间或用户更改密码后保持不变.)
- 数据被保存到数据库中(我无法读取它,因为它是用用户凭据加密的)
- 用户在不同的设备上登录(可以立即生成解密密钥并可以解密数据.)
推荐答案
您可以通过以下方式轻松做到:
You can easily do that the following way:
- 用户A登录后,手机上会随机生成一对公私钥.例如:使用 Ecc Curve25519
- A 的私钥安全地存储在他的手机上
- 来自 A 的公钥存储在 firebase 中,任何与 A 聊天的人都可以访问.
- 如果 X 向 A 发送消息,他从 Firebase 中从 A 获取公钥,然后在本地为 A 加密消息,并将加密后的消息存储在来自 A 的收件箱中的 firebase
- A 从 firebase 下载加密消息,并使用存储在手机上的私钥对其进行解密
(A 到 X 反之亦然)
(vice versa for A to X)
如果 A 想要转移到另一部手机或想要使用多部手机,您可以这样做:
If A want's to move to another phone or wants to use multiple phones you can do this that way:
- 让 A 定义一个强密码来加密他本地存储的私钥.(或创建一个随机密码并使用二维码进行密钥交换)
- 使用步骤 1 中的密码在他的手机上本地加密私钥(例如:使用 AES256)并将其上传到 firebase.(可选用他的私钥签名)
- 从 A 的第二台设备下载加密的私钥
- 在第二台设备上向 A 询问密码并安全地存储私钥(可选择使用来自 A 的公钥检查签名)
- 如果不需要备份,请从 firebase 中删除加密的私钥
这篇关于如何在 Firebase 中加密用户数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!