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:
- 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. - 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. - When you want to
decrypt
this hash, post therealData
andrandomAddress
to a smart contract. Hash them on-chain using solidity'skeccak256
function.keccak256(abi.encodePacked(uint256 realData, address randomAddress))
. - If the on-chain hash matches the
data
variable, then this data has effectively beendecrypted
.
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.