Skip to content

Instantly share code, notes, and snippets.

@PseudoSky
Last active December 16, 2015 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PseudoSky/f4372d13281178f0f1d6 to your computer and use it in GitHub Desktop.
Save PseudoSky/f4372d13281178f0f1d6 to your computer and use it in GitHub Desktop.
How to use sjcl.js for ECDH and Shared Key Exchange
function User(username, key_pair){
this.username = username;
/* If no existing keys generate them */
if(!key_pair){
this.keys = sjcl.ecc.elGamal.generateKeys(192,0);
}else{
this.keys = key_pair
}
/* Stores Username -> Pub Key */
this.session_keys = {};
/* Publicly Transfer The Users DH public key to the recipient */
this.send_key_to_user = function(user_recipient){
log("User: "+this.username+" sending key to "+user_recipient.username);
user_recipient.recieve_key(this.keys.pub, this.username);
}
/* Recieve the sender's DH public key and store it */
this.recieve_key = function(key,username){
log("User: "+this.username+" recieved key from "+username);
this.session_keys[username] = key;
}
/*
Computes the DH shared key from User's sec (secret key)
and the recipientes pre shared pub key
*/
this.get_shared_key = function(username){
return this.keys.sec.dh(this.session_keys[username])
}
/* Assume the user has the recipients username & key */
this.send_message = function(user_recipient, clear_text){
log( "User: " + this.username + " sending message to " + user_recipient.username,"\""+clear_text+"\"" );
var shared_key = this.get_shared_key(user_recipient.username);
log("User: "+this.username+" and User: "+user_recipient.username+" Have the shared key:",shared_key);
/* Encrypt the clear_text message using the shared session key */
var cipher_text = sjcl.encrypt(shared_key, clear_text);
log( user_recipient.read_message(cipher_text,this.username) );
return cipher_text;
}
/*
Gets the user's shared key using their username
Decrypts the text using that key
*/
this.read_message = function(plain_text, username){
log("User: "+ this.username +" read a message from " + username+" With encrypted content: ",plain_text);
var shared_key = this.get_shared_key(username)
var message_clear_text = sjcl.decrypt(shared_key,plain_text);
log("The message in clear text read:",message_clear_text)
}
}
var log = (...args) => args.forEach( a => console.log(a) )
var Bob = new User('Bob');
var Alice = new User('Alice');
Bob.send_key_to_user(Alice);
Alice.send_key_to_user(Bob);
Bob.send_message(Alice, "I wonder if Eve can hear us");
Alice.send_message(Bob, "I hope not, I hate Eve!");
function User(username, key_pair){
this.username = username;
/* If no existing keys generate them */
if(!key_pair){
this.keys = sjcl.ecc.elGamal.generateKeys(192,0);
}else{
this.keys = key_pair
}
/* Stores Username -> Pub Key */
this.session_keys = {};
/* Publicly Transfer The Users DH public key to the recipient */
this.send_key_to_user = function(user_recipient){
user_recipient.recieve_key(this.keys.pub, this.username);
}
/* Recieve the sender's DH public key and store it */
this.recieve_key = function(key,username){
this.session_keys[username] = key;
}
/*
Computes the DH shared key from User's sec (secret key)
and the recipientes pre shared pub key
*/
this.get_shared_key = function(username){
return this.keys.sec.dh(this.session_keys[username])
}
/* Assume the user has the recipients username & key */
this.send_message = function(user_recipient, clear_text){
var shared_key = this.get_shared_key(user_recipient.username);
/* Encrypt the clear_text message using the shared session key */
return sjcl.encrypt(shared_key, clear_text);
}
/*
Gets the user's shared key using their username
Decrypts the text using that key
*/
this.read_message = function(plain_text, username){
var shared_key = this.get_shared_key(username)
return sjcl.decrypt(shared_key,plain_text);
}
}
var Bob = new User('Bob');
var Alice = new User('Alice');
Bob.send_key_to_user(Alice);
Alice.send_key_to_user(Bob);
Bob.send_message(Alice, "I wonder if Eve can hear us");
Alice.send_message(Bob, "I hope not, I hate Eve!");
@PseudoSky
Copy link
Author

Lemme know if I did something incorrectly... The sjcl library is a bit confusing.
sjcl.js Crypto Library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment