Im wrting a dapp which holds / manages sensitive data which might need to be encrypted and decrypted and push again into chain.

I know it is not safe to encrypt data in smart contract,which is why Im thinking of off-chain encryption mechanism . I thought to encrypt data using public key and then decrypt using private key . However I found that popular wallet s/w like metamask doesnt allow this.

1.What are the possible ways to acheive this or any other better ways? (I thought managing users eth keys is not so safe)

2.If using custom keys for encryption, Is it ok using a keymanagement tool like Google KMS or Amazon Kms to manage keys and encrypt data?


You can encrypt data with the public key and decrypt it with the private key offchain with standard encryption, in particular using Elliptic Curve Integrated Encryption Scheme (ECIES)

An example in python using ethereum keys:

In Rust:

In javascript:

In python, pyelliptic is sometimes a pain in the ass, you can obtain the same result following this:

  • To encrypt data off-chain , we need public and private keys . But how do we get them ? As mentioned in question, Popular wallet programs like metamask will neither allow to access keys nor will provide encrypt / decrypt methods . Isnt it complex to write own wallet program which manages keys and off-dapp functionality? – kulakarni dheeraj Feb 12 at 12:54
  • Yeah, you cannot encrypt a content without having access to the private key, unless the wallet you are using has support for encryption, but it's unlikely as there are multiple algorithms. As far as I know, the only one that comes in to my mind that supports ecies encryption and decryption is Parity. You would need to write your own wallet and it's complex but really doable as you got a lot of open source examples around. – waytosay Mar 11 at 15:36

MetaMask does not support encryption/decryption but is support off-chain signing that you can verify on-chain.

The signature will use the user private key. This way it is 100% authentic.

For the data, you can save off-chain or on-chain. But instead of encryption and decryption, you have to use ask the user to sign and you can later verify the signature on and off-chain.

Actually, there is already the EIP712 standard for this. You can check how it works with MetaMask here: https://medium.com/metamask/eip712-is-coming-what-to-expect-and-how-to-use-it-bb92fd1a7a26

Bellow is how the user is requested to sign a specific data: enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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