To decrypt the encrypted data you can use a function from eccrypto called decrypt()
. I wrote these functions to help you understand the possibilities. The encrypt and decrypt function are asynchronous
, which means we can await
them in an async
function. You have to create a public and private key and pass them to the function, that way encryption / decryption is possible.
const eccrypto = require("eccrypto");
// generate keys
const privateKeyA = eccrypto.generatePrivate();
const publicKeyA = eccrypto.getPublic(privateKeyA);
const privateKeyB = eccrypto.generatePrivate();
const publicKeyB = eccrypto.getPublic(privateKeyB);
// function for encrypting the message you pass in
async function encryptingData(pub_key, msg) {
return await eccrypto.encrypt(pub_key, Buffer.from(msg));
}
// function for decrypting the encrypted message
async function decryptingData(priv_key, encryptedmsg) {
return await eccrypto.decrypt(priv_key, encryptedmsg);
}
// example main function on how you could use seperate functions.
async function communicate() {
const message_a = await encryptingData(publicKeyA, 'Message a');
const message_b = await encryptingData(publicKeyB, 'Message b');
const decrypted_a = await decryptingData(privateKeyA, message_a);
const decrypted_b = await decryptingData(privateKeyB, message_b);
console.log(`Message a encrypted : ${JSON.stringify(message_a)}.`);
console.log(`Message a decrypted: ${decrypted_a}.`);
console.log('-------------------------------------')
console.log(`Message b encrypted : ${JSON.stringify(message_b)}.`);
console.log(`Message b decrypted : ${decrypted_b}.`);
}
communicate();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…