Skip to content

Instantly share code, notes, and snippets.

@alexneamtu
Created February 11, 2022 21:13
Show Gist options
  • Save alexneamtu/74d3fb0e5dae93d0fbcd0f7a5a909353 to your computer and use it in GitHub Desktop.
Save alexneamtu/74d3fb0e5dae93d0fbcd0f7a5a909353 to your computer and use it in GitHub Desktop.
Ecies
const { privateDecrypt } = require('crypto');
const eccrypto = require('eccrypto');
const oldPrivateKey = '-----BEGIN PRIVATE KEY-----\n' +
///PRIVATE KEY CODE
'-----END PRIVATE KEY-----';
const data = 'some base64';
const decryptpedDoc = privateDecrypt(oldPrivateKey, Buffer.from(data, 'base64'));
const decryptpedDocString = decryptpedDoc.toString();
const privateKeyA = eccrypto.generatePrivate();
console.log(privateKeyA.toString('hex'));
const publicKeyA = eccrypto.getPublic(privateKeyA);
console.log(publicKeyA.toString('hex'));
const privateKeyB = eccrypto.generatePrivate();
console.log(privateKeyB.toString('hex'));
const publicKeyB = eccrypto.getPublic(privateKeyB);
console.log(publicKeyB.toString('hex'));
// Encrypting the message for B.
eccrypto.encrypt(publicKeyB, Buffer.from(decryptpedDocString)).then(function (encrypted) {
// B decrypting the message.
eccrypto.decrypt(privateKeyB, encrypted).then(function (plaintext) {
console.log('Message to part B:', plaintext.toString());
});
});
// Encrypting the message for A.
eccrypto.encrypt(publicKeyA, Buffer.from(decryptpedDocString)).then(function (encrypted) {
// A decrypting the message.
eccrypto.decrypt(privateKeyA, encrypted).then(function (plaintext) {
console.log('Message to part A:', plaintext.toString());
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment