Chat

Building a Key Exchange System to Send Encrypted Messages

3 min read Michael Carroll on Jul 23, 2014

Good News

Good News! We’ve launched an all new Chat Resource Center.

We recommend checking out our new Chat Resource Center, which includes overviews, tutorials, and design patterns for building and deploying mobile and web chat.

Take me to the Chat Resource Center →

Key Exchange System for Sending Encrypted MessagesIn this blog post, we’ll cover how to build a key exchange system with PubNub, and once we do so, we’ll be able to send encrypted messages between users.

Here’s what we’ve covered so far:

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.key exchange

Step 1: Import the PubNub JavaScript SDK

You’ll first need to sign up for a PubNub account. Once you sign up, you can get your unique PubNub keys in the PubNub Developer Portal. Then, import the PubNub JavaScript SDK and to initialize PubNub with your unique keys, as well as SSL enabled and with 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.

Integrating Presence

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 state to 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 here_now.

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.

0