27

Is there any way to store info on a smart contract, and keep it a secret?

If a contract has a state variable that is not public like:

contract foo{
    uint public publicVariable;
    uint notPublic;
}

is "notPublic" truly not public? what if it contains secret info like a credit card #? is that ok? if I wanted to keep some top secret info in that variable is there a way to do it so no one can get it even though it is onchain?

1

3 Answers 3

25

Everything on the Ethereum blockchain is public.

Therefore you cannot obscure data. This is because each Ethereum node stores a copy of the entire blockchain, and so each node could theoretically inspect their copy to find whatever data you wanted to be obscured.

That doesn't mean you can't put sensitive data on the blockchain, you just have to encrypt it first.

Encrypt data with a public key

If you want to store sensitive user information on the blockchain you should encrypt the data with their public key. Only the user with their private key can decrypt it.

This can seem like more work then you have to do with more normal web application, but it's actually a benefit. If you assume your data can be accessed by an attacker, you will avoid many security pitfalls that cause these huge data breaches. Security by obscurity is not really security at all!

4
  • How would I approach such encryption without knowing the receiver beforehand?
    – floAr
    Mar 29, 2017 at 13:15
  • I'll second Karl's question - has anyone seen good examples that could accomplish the following: 1. Encrpyt the non-public data 2. Later, let a new party have the data, all done through the smart-contract or blockchain (aka without needing some third offline transfer of information) Any thoughts? Sep 18, 2017 at 20:51
  • I am also interested in that particular problem. But apparently there is no way to accomplish that without some off-chain help (such as keep). Let me know if you come up with something.
    – vaz
    Oct 2, 2017 at 12:42
  • theoretically inspect their copy to find whatever data you wanted to be obscured. how can one do it? @Karl
    – Aniket
    May 3, 2018 at 12:20
13

You can encrypt the data and keep the keys off-chain:

See: https://github.com/brix/crypto-js

4
  • Ok this is good. But what about that "notPublic" variable? how would someone even be able to access something like that? I don't understand what is wrong with it
    – zunior
    Apr 3, 2016 at 20:32
  • 1
    the whole state of the blockchain is public, and so are every transaction and interaction with it. If its stored on chain once could simply lookup the contracts state or simulate all incomming transactions etc. there area a lot of ways. making the variable not public just don't generate an interface. See for example live.ether.camp where you can "follow" each transaction
    – Denis
    Apr 3, 2016 at 21:56
  • 1
    @zunior All that the 'public' part changes is code related. When a variable is 'public' it can be accessed by any global Function() for example. If it is private it can be accessed only locally.
    – cpl
    Apr 5, 2016 at 4:51
  • @thee-engineer Even if it is private, malicious miner still have ways to steal it. Nov 7, 2018 at 18:36
1

One problem with encrypting data and putting it on chain is that you're leaving your encrypted data on a public immutable data store forever. What if some radical new computing paradigm (quantum?) can break the encryption, or your keys are stolen?

This problem is one that the Baseline Protocol is aiming to solve, see:

https://docs.baseline-protocol.org

In short, it aims to use Zero Knowledge Proofs (ZKP) to NOT store your private information on-chain, but instead to store a proof that you know the information without revealing the information.

Your Answer

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

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