0

I am learning the basics of eccrypto - JavaScript Elliptic curve cryptography library. I have got the following code from documentation.

var eccrypto = require("eccrypto");
    
var privateKeyA = eccrypto.generatePrivate();
var publicKeyA = eccrypto.getPublic(privateKeyA);
    
// Encrypting the message for A.
eccrypto.encrypt(publicKeyA, Buffer.from("msg to a")).then(function (encrypted) {
    console.log(encrypted)
    // A decrypting the message.
    eccrypto.decrypt(privateKeyA, encrypted).then(function (plaintext) {
    console.log("Message to part A:", plaintext.toString());
    });
});

I am afraid that I can't understand the keys of "encrypted" object properly.

There are four keys in that object : "iv", "ephemPublicKey", "ciphertext" and "mac".

How can I get the cncrypted text from this object in binary format?

Progman
  • 13,123
  • 4
  • 28
  • 43
Shah Shishir
  • 133
  • 2
  • 11
  • 1
    You get the encrypted text as a parameter for the function, provided at the `.then(...);` part. – Progman Sep 21 '19 at 21:31

1 Answers1

1

eccrypto (and any other ECIES lib) needs those parameters to decrypt data. You can read more about "Integrated Encryption Scheme" in this survey. In short "iv" is the initialization vector which could be a random buffer or something special that you can generate yourself. "ephemPublicKey" is the sender's ephemeral public key buffer. "ciphertext" is the encrypted message buffer. Finally "mac" buffer helps to check integrity of the message (checksum).

If you want to store this encrypted data you could convert them into a hex or base64 or whatever string type fits your needs. For instance you can convert this object to JSON using JSON.stringify(encrypted) and then store it as is or convert it to a 'base64' string to take less space. Another solution which is already applied in some other libraries is to concat all those buffers in a single buffer like this Buffer.concat(encrypted.ephemPublicKey, encrypted.iv, encrypted.ciphertext, encrypted.mac).toString('base64') now when you want to decrypt this string you should first convert it back to a buffer and split those buffers.

Mohammad Rafigh
  • 660
  • 9
  • 14