I have a key pair:

The data is encrypted off-chain:

and it is saved into the following smart contract:

contract Foo {
    string data;

Can I decrypt stored data on-chain, assuming I provide a corresponding private key?

function decryptData(string privKey){

I don't think this is possible in the present arrangement.

SK1 would have to be transmitted to the contract, therefore revealed to all verifiers, which is everyone. In any case, that's the hurdle. In summary, if the contract can decode it, then everyone can decode it.

This is the sort of thing that might be possible with a ZKSnarks implementation, as I understand it.

This might be of assistance: Can smart contracts compute on encrypted data?

Hope it helps.

  • I read the question as being whether you can do the decryption, not whether you can do it and keep the private key and the result secret. There are various reasons you might want to do this. – Edmund Edgar Mar 16 '17 at 22:03
  • @EdmundEdgar You might be right. – Rob Hitchens B9lab Mar 16 '17 at 22:16
  • Answer below. In any case, what exactly are you trying to achieve? Proving that someone holds the right key private? – Giuseppe Bertone Mar 17 '17 at 1:43

Yes, you can, at least in principle.

Ethereum is Turing-complete, so you can do whatever calculations you like. However, I don't know of any actual implementations that do this on-chain, and depending on the details of your encryption algorithms it may not be practical to do it within the gas limit. The upcoming Metropolis system upgrade may help, as it should include biginteger functionality that will make it easier and cheaper to do crypto stuff on-chain.

Whether this is a good idea is another question, though, depending on what you're trying to achieve. The private key and the decrypted data will be visible to anyone who can access the blockchain, and for most purposes this is the kind of thing you want to do off-chain in any case.

  • What if the decryptData function is a call and does not publish or broadcast anything on the blockchain? In this case the private key and encrypted data will not be visiable to anyone. Or do I miss something? – jim Aug 9 '17 at 15:00
  • Well, you could write EVM code to do the decryption, store the code on the blockchain and run it only locally on your own node. I'm not sure whether would count as "on the blockchain" though... – Edmund Edgar Aug 9 '17 at 21:09
  • If I use Owner modifier as described in the Solidity documentation will be encrypted inside such that even though data is public all they is gibberish ? – siva Nov 1 '17 at 12:22

No, you can't. Ethereum EVM does not provide encryption/decryption functions, they are very high level general purpose functions. But you can try to decrypt outside of the smart contract, using the not-anymore-private key found in the transaction.

See also What are effective techniques to encrypt/decrypt data stored in a smart contract?

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.