Real-time Chat Blog

Building a Key Exchange System to Send Encrypted Messages

3 min readPubNub Staff on Jul 23, 2014
Try PubNub Today

Free up to 1MM monthly messages. No credit card required.

Subscribe to our newsletter

By submitting this form, you are agreeing to our Terms and Conditions and Privacy Policy.

How to build a key exchange system using PubNub, enabling users to send encrypted, self destructing chat messages in real time.

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 PubNub 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 PubNub Presence

PubNub 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 PubNub 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.

More from PubNub

Can Empathy Exist in the Metaverse
News May 16, 20221 min read

Can Empathy Exist in the Metaverse

A roundtable discussion led by PubNub’s COO, Casey Clegg, exploring the topics of what it means to be human in a virtual world.

PubNub Staff

PubNub Staff

How to Advance Telehealth and Virtual Care Technologies
News May 2, 20221 min read

How to Advance Telehealth and Virtual Care Technologies

Dr. Joe Kvedar, Chair of the Board for the American Telemedicine Association, joins our COO, Casey Clegg, to discuss why...

PubNub Staff

PubNub Staff

Another Step Towards Data Security: ISO-27001 Implementation
BuildMay 2, 20221 min read

Another Step Towards Data Security: ISO-27001 Implementation

Today, we are glad to announce that we are currently in the process of implementing ISO-27001 security standards.

PubNub Staff

PubNub Staff