19

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?

22

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!

  • How would I approach such encryption without knowing the receiver beforehand? – floAr Mar 29 '17 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? – Tommy Nicholas Sep 18 '17 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 '17 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 '18 at 12:20
12

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

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

  • 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 '16 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 '16 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. – thee-engineer Apr 5 '16 at 4:51
  • @thee-engineer Even if it is private, malicious miner still have ways to steal it. – lllllllllllll Nov 7 '18 at 18:36

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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