2

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){
    ...
}

4 Answers 4

2

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.

4
  • 1
    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. Commented Mar 16, 2017 at 22:03
  • @EdmundEdgar You might be right. Commented Mar 16, 2017 at 22:16
  • Answer below. In any case, what exactly are you trying to achieve? Proving that someone holds the right key private? Commented Mar 17, 2017 at 1:43
  • The question asks about decryption, not encryption. Commented Mar 2, 2019 at 20:01
1

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.

3
  • 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
    Commented Aug 9, 2017 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... Commented Aug 9, 2017 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 ?
    – NaN
    Commented Nov 1, 2017 at 12:22
0

If you're trying to decrypt data on-chain, I recommend using a hashing function on your data appended to a random ethereum address for entropy. Here's how:

  1. Post hashed data inside your given data variable on-chain. For all intents and purposes, this data is encrypted, as nobody but you knows the hash inputs that generated this hash output.
  2. This data is a hash made by: web3.utils.soliditySha3([realData, randomAddress]) off-chain inside Web3js. You keep both these inputs a secret from the blockchain.
  3. When you want to decrypt this hash, post the realData and randomAddress to a smart contract. Hash them on-chain using solidity's keccak256 function. keccak256(abi.encodePacked(uint256 realData, address randomAddress)).
  4. If the on-chain hash matches the data variable, then this data has effectively been decrypted.

Originally I wrote my answer using only signatures but that was wrong. You cannot use a signature function for this (it also costs more gas anyway). For more information: https://crypto.stackexchange.com/questions/43894/asymmetry-between-public-key-and-private-key-digital-signatures-use-the-private

If you want to make sure the hash came from an authorized user, use a mapping object to store the hashes based on a msg.sender variable. msg.sender is effectively a signature as it is.

-1

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?

0

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.