I am making a chat application where:
- A user can be in a group with multiple other users.
- All messages must be encrypted with end-to-end
I am using firebase-realtime-database
to store data.
What I have
Randomly generated secret key for every user
String generateEncryptionKey(int len) { var r = Random(); String randomString = String.fromCharCodes(List.generate(len, (index) => r.nextInt(33) + 89)); return randomString; }
This is probably wrong. I would need clarification if I need to get a derived key
from the user's password or if this is sufficient.
What I need
- Alice sends a message: I need it to be encrypted with a key that is also accessible by any other participant in the group (
shared-secret key
) - Bob wants to read this message: I need to decrypt this with the
shared-secret key
The shared-secret key
shares something in common with every personal key, right? So the messages encrypted by any of the participants can be decrypted by any other.
How can I generate the shared-secret key
?
Code Blocks Needed
generateKeyPair() {
// Generate a private - public keypair for each user
// ...somehow used to make the `shared-secret key`?
}
generateSharedSecretKey() {
// Saved to database as the group's shared secret key.
// only participants can use it to decrypt messages
}
sendMessage() {
// 1. Encrypt the message (**code needed**)...using
// which combination of keys?
// 2. Save to database (I already handle this)
}
receiveMessage() {
// 1. Read from database (I already handle this)
// 2. Decrypt the message (**code needed**)...using
// which combination of keys?
}
Considerations
- Multiple users (so many keys)
- It doesn't need to be super secure (just the bare minimum is enough)
- A user can have an account on multiple devices
I have read about the Diffie–Hellman key exchange, key pairs, etc. But I don't really understand all the concepts as I am quite new.
I would need:
- Clarification on basic end-to-end encryption concepts (in case something in my explanation was wrong)
- Simple code samples of every key generation step
- The code blocks have to work no matter which user is sending a message or receiving. That dynamism is what confuses me. How can you encrypt something taking into account that any of 50 users can read it?
I just want encryption of simple data (strings) with multiple users through a database. Is there is an easier way than end-to-end?