6

Is it possible to add an assymetric encryption options to the library so it is possible to encrypt the data using one's public key and then decrypt the data using one's private key using the same algorithm as the one used for signing the transactions? Maybe with eosjs-ecc?

In eosjs-ecc there is only symmetric encryption available when both keys are needed

2
  • You can request feature via github.com/eosio/eos issue tracker.
    – friedger
    Jun 11 '18 at 8:13
  • The request was rejected. They stated that the library is not meant to encrypt the data, but just to sign them Jun 13 '18 at 12:41
2

Unlike RSA, ECC isn't able to encrypt and decrypt data on its on. If you wish to use the keys to encrypt and decrypt bulk data, you need to combine it with other cryptographic technologies. The common ways to do this is with Elliptic Curve Integrated Encryption Scheme (ECIES).

ECIES uses Elliptic Curve-Diffie Hellman (ECDH) to generate a shared key that is used to encrypt the data. It uses the other user's public key, in conjunction with your private key, to perform a DH key exchange to generate a shared secret that you can use to encrypt the message, and which the recipient can use to decrypt it.

1
1

Here is one way to do it using the standard-ecies library

import * as ecc from 'eosjs-ecc';
import * as ecies from 'standard-ecies';

export function testEncryptDecyptMessage() {

  const wif = "5JgbL2ZnoEAhTudReWH1RnMuQS6DBeLZt4ucV6t8aymVEuYg7sr";
  const pubKey = "EOS6hMjoWRF2L8x9YpeqtUEcsDKAyxSuM1APicxgRU1E3oyV5sDEg";

  // Encrypting the message for B.
  const bufEncrypted = encryptMessage(pubKey, 'hello world');

  // Decrypting the message.
  const msgBuffer = decryptMessage(wif, bufEncrypted);

  console.log("Decrypted Message:", msgBuffer.toString());
  console.log();
}

export function decryptMessage(wif: string, encrypt: Buffer | string): string {
  const ecdh = crypto.createECDH('secp256k1');
  const privBuffer = ecc.PrivateKey(wif).toBuffer();
  assert(privBuffer.length == 32);
  ecdh.setPrivateKey(privBuffer);

  const encryptBuffer: Buffer = typeof encrypt == 'string' ? Buffer.from(encrypt, "hex") : encrypt;
  return ecies.decrypt(ecdh, encryptBuffer);
}

export function encryptMessage(pubKey: string, message: string | Buffer): Buffer {
  const pubBuffer = ecc.PublicKey(pubKey).toUncompressed().toBuffer();
  assert(pubBuffer.length == 65);

  const messageBuffer = (typeof message == "string" ? Buffer.from(message) : message);
  const encryptedBuffer: Buffer = ecies.encrypt(pubBuffer, messageBuffer);

  return encryptedBuffer;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.