Created
September 27, 2012 20:05
-
-
Save dacbd/3796173 to your computer and use it in GitHub Desktop.
SJCL ecc encrypt/decrypt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
ibmibmibm
commented
Sep 19, 2014
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment