Building a Key Exchange System to Send Encrypted Messages
Here’s what we’ve covered so far:
- Part One: Set up a chat application and gave an overview of its architecture.
- Part Three: Walked you through how to send TLS encrypted chat messages.
- Part Four: How to send self destructing chat messages
- Part Five: Building a chatroom UI
This is a five part series on building Babel, an open source chat widget and API built with PubNub. Babel allows you to send and receive self destructing, encrypted messages and exchange 1024-bit RSA public keys in a chatroom.
We have a live working Babel self destructing chat demo here. You can also take a look at the source code on our Babel Github Repository. Now, let’s get the tutorial started!
Building a Key Exchange System
Exchanging public keys is a fundamental component of Babel and developing self destructive messages. Once two users have exchanged public keys, they can then send encrypted messages to one another.
So how does Babel use PubNub to exchange public keys? Let’s walkthrough how you can build your own key exchange system with PubNub.
uuid set to a username that other users will be able to identify us with..
Step 2: Public Key Sharing
Our next step is to get a public key to be shared. For the purposes of this blog post, we’ll just use a random string to substitute for our public key. However, a more realistic public key string would be something like the Base64 encoding of an RSA public key.
After we get a public key, we can share it with the Presence feature. To do this, we first subscribe to a PubNub channel and set our
state to an object that contains our username and public key.
Presence allows users to see who is online and offline, and updates in real time as users log on and off. After subscribing to a channel, other users will be able to see our
state with Presence.
here_now gets a list of unique user-ids currently subscribed to the channel, the total occupancy of the channel, and also the
state information of all users subscribed to the channel since we set
true in our function call.
The data returned by
here_now to the callback will look something like this.
Thus, to get the
state of all the users subscribed to the channel, you can loop through the
uuids array from the the data returned by
As you can see, exchanging your public keys with PubNub is really easy! However, we’re not done yet. In future blog posts we’ll go over how to send encrypted messages and self-destructing messages through PubNub.