Skip to content

Instantly share code, notes, and snippets.

@dacbd
Created September 27, 2012 20:05
Show Gist options
  • Save dacbd/3796173 to your computer and use it in GitHub Desktop.
Save dacbd/3796173 to your computer and use it in GitHub Desktop.
SJCL ecc encrypt/decrypt
var data = {
herp: "derp"
};
//Make the keys.
var temp = sjcl.ecc.elGamal.generateKeys(384, 1);
//to encrypt with publickey:
var pubjson = temp.pub.serialize();
var point = sjcl.ecc.curves["c" + pubjson.curve].fromBits(pubjson.point);
var publicKey = new sjcl.ecc.elGamal.publicKey(pubjson.curve, point.curve, point);
var symkey_obj = publicKey.kem(0);
var ciphertext = sjcl.encrypt(symkey_obj.key, JSON.stringify(data));
var message = JSON.stringify({ 'ciphertext': ciphertext, 'encrypted_key': symkey_obj.tag });
// and message gets saved off
//to decrypt with privatekey/recovering "message"
var cipherMessage = JSON.parse(message);
var secjson = temp.sec.serialize();
var ex = sjcl.bn.fromBits(secjson.exponent);
var privatekey_obj = new sjcl.ecc.elGamal.secretKey(secjson.curve, sjcl.ecc.curves["c" +secjson.curve], ex);
var symkey = privatekey_obj.unkem(cipherMessage.encrypted_key);
var decryptedData = sjcl.decrypt(symkey, cipherMessage.ciphertext);
@jamilbk
Copy link

jamilbk commented Feb 14, 2014

I'm also interested in an updated version of this. serialize() is no longer part of the ecc library

@ibmibmibm
Copy link

var sjcl = require('sjcl');

var data = {
    herp: "derp"
};

var curve = sjcl.ecc.curves.c384;
var temp = sjcl.ecc.elGamal.generateKeys(curve, 1);
var pub = temp.pub.get();
var sec = temp.sec.get();

var pubObj = new sjcl.ecc.elGamal.publicKey(curve, new sjcl.ecc.point(curve, sjcl.bn.prime.p384.fromBits(pub.x), sjcl.bn.prime.p384.fromBits(pub.y)));
var secObj = new sjcl.ecc.elGamal.secretKey(curve, sjcl.bn.prime.p384.fromBits(sec));

var ciphertext = sjcl.encrypt(pubObj, JSON.stringify(data));
var message = JSON.stringify({'ciphertext': ciphertext});

var cipherMessage = JSON.parse(message);
var decryptedData = sjcl.decrypt(secObj, cipherMessage.ciphertext);
console.log(decryptedData);

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