Skip to content

Commit

Permalink
Merge c8260f6 into 053f700
Browse files Browse the repository at this point in the history
  • Loading branch information
Braydon Fuller committed Jan 24, 2015
2 parents 053f700 + c8260f6 commit 9226294
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions lib/ecies.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

var bitcore = require('bitcore');

var PublicKey = bitcore.PublicKey;
var Hash = bitcore.crypto.Hash;
var Random = bitcore.crypto.Random;
var $ = bitcore.util.preconditions;
Expand Down Expand Up @@ -59,8 +60,8 @@ ECIES.prototype.decrypt = function(encbuf) {
$.checkArgument(encbuf);

var kB = this._privateKey.bn;
var frompubkey = this._publicKey;
var R = frompubkey.point;
this._publicKey = PublicKey.fromDER(encbuf.slice(0, 33));
var R = this._publicKey.point;
var P = R.mul(kB);
var S = P.getX();

Expand Down
14 changes: 10 additions & 4 deletions test/ecies.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,25 +47,31 @@ describe('ECIES', function() {
var alice = ECIES()
.privateKey(aliceKey)
.publicKey(bobKey.publicKey);

var bob = ECIES()
.privateKey(bobKey)
.publicKey(aliceKey.publicKey);

var message = 'attack at dawn';
var encrypted = '0339e504d6492b082da96e11e8f039796b06cd4855c101e2492a6f10f3e056a9e7499368e41313fa48f71759d13469c28b0bd6ff03a08b2ae5e6679e848f92db4b26a8ccf9c0b43f16eae6216c36380f70724743fe9053c98d94281aa74c94df40';
var encBuf = new Buffer(encrypted, 'hex');

it('correctly encrypts a message', function() {
var encrypted = alice.encrypt(message);
Buffer.isBuffer(encrypted).should.equal(true);
});

it('correctly decrypts a message', function() {

var encrypted = '0339e504d6492b082da96e11e8f039796b06cd4855c101e2492a6f10f3e056a9e7499368e41313fa48f71759d13469c28b0bd6ff03a08b2ae5e6679e848f92db4b26a8ccf9c0b43f16eae6216c36380f70724743fe9053c98d94281aa74c94df40';

var encBuf = new Buffer(encrypted, 'hex');
var decrypted = bob
.decrypt(encBuf)
.toString();
decrypted.should.equal(message);
});

it('retrieves senders publickey from the encypted buffer', function() {
var bob2 = ECIES().privateKey(bobKey);
var decrypted = bob2.decrypt(encBuf).toString();
bob2._publicKey.toDER().should.deep.equal(aliceKey.publicKey.toDER());
decrypted.should.equal(message);
});

Expand Down

0 comments on commit 9226294

Please sign in to comment.