8

I have user specific data in a table on my smart contract that I don't want others to see. Can I encrypt that data with a public key of the user? If possible then how to encrypt that data with the public key? Then how will the user decrypt that data with their private key?

8
  • Do you want to retrieve the public key directly from the smart contract? Then, what encryption would you like to use? Try to look at this link: github.com/EOSIO/eosjs-ecc/issues/19 Sep 15, 2018 at 12:34
  • if you do't want other to see your table and table is not a abi action define your table private , and if table is abi action than you can put the required authority to yourself.Encryption is a better option but it may cost you more ram (as encription require computation ) Sep 22, 2018 at 5:23
  • //@abi table tb i64 struct table { uint64_t id; uin8_t num; unit64_t primary_key() const { return id ;}}; . I want to hide the num variable so that others cant get this using cleos get table command line .Also this piece of code is in private sction of my contract line .So any way to prevent others from getting the value of num variable?
    – kstnew
    Sep 23, 2018 at 6:16
  • //@abi table tb i64 struct table { uint64_t id; uin8_t num; unit64_t primary_key() const { return id ;}}; . I want to hide the num variable so that others cant get this using cleos get table command line .Also this piece of code is in private part of my contract class .So any way to prevent others from getting the value of num variable?
    – kstnew
    Sep 23, 2018 at 6:31
  • @NirdeshKumarChoudhary What do you mean when you say define the table as private? Sep 28, 2018 at 10:26

2 Answers 2

3

Yes, but you would encrypt and decrypt off-chain while only storing the encrypted data and other necessary data in the blockchain state.

With eosjs, you can use the following set of commands to encrypt and decrypt data:

// The message that will be encrypted
let message = "my message"

// For example purposes, this is the keypar of the recipient. Note I removed
// EOS from the public key prefix
let someonesPrivateKey="5JCgCpHE9ZjXYUaCXSZ39HwaFEBvQE9DmU7R79Dha8roARasmZw"
let someonesPublicKey="76xGfmsKEYDdgCjd4tLXRBXBNf8hTEKxPfTc7Tt7q878gDFzEG"

// Create another keypair for this encryption.
// The recipient needs to know the public key
let myPrivate=ecc.PrivateKey.fromSeed('my')
let myPublic=myPrivate.toPublic()

// Encrypt the message using both my private key and the other party's public key
let encryptedMessage = ecc.Aes.encrypt(myPrivate, someonesPublicKey, message)

// Decrypt the message using th other party's private key and your public key
console.log(ecc.Aes.decrypt(someonesPrivateKey, myPublic,
      encryptedMessage.nonce, encryptedMessage.message, encryptedMessage.checksum))

Much of the code is from here

Thus, you can store encryptedMessage.nonce, encryptedMessage.message, and encryptedMessage.checksum in the RAM, log, or IPFS, and only the party who knows the private key corresponding to the public key you used for encryption can decrypt the message (they'd need to know the public key used for encryption as well, which can be stored in the state/log/ipfs/etc.)

The diagram below from this blogpost illustrates the process performed.

enter image description here

1

Some sexy dude made this library https://github.com/dtrades/dtrades-communication

Let's you encrypt a string with a public key, decrypt with the corresponding private.

import { encrypt, decrypt } from 'eos-communication-lib';

const public_key = "EOS6MRyAjQq8ud7hVNYcfnVPJqcVpscN5So8BhtHuGYqET5GDW5CV";
const private_key = "5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3";

const message = "Private Message, shhhh!";
const encrypted = encrypt(private_key, public_key, message);
// => TO DECRYPT: eos-communication
// .1167451677...23460624..862584768Q+h1AeLQbjfzZJD1Nsx6kk3U/jSNStwoWstz9uNCadw=

const decrypted = decrypt(private_key, public_key, encrypted);
// => Private Message, shhhh!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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