Say I have a hash of a file which is stored on IPFS.I want to encrypt the hash with public keys of few people.

How can I do the encryption of the hash (encrypted hash should be stored on smart contract) using react web application.

And can others decrypt the encrypted hash ,to get the actual IPFS document ,using their private key (Asking a user to enter their private key in a web app can be a bad practice) ,can we do this using decryption using Metamask or any?

1 Answer 1


eth-crypto will do the job for you, you just need the list of the public keys of the people who are allowed to decrypt the encrypted content.

Here is example how you encrypt content with public key:

async function encryptViaPublicKey() {
    const encrypted_obj = await EthCrypto.encryptWithPublicKey(<PUBLIC_KEY>, <SOME_TEXT>);
    const encrypted_string = EthCrypto.cipher.stringify(encrypted_obj);

And example how to decrypt the encrypted text via private key:

async function decryptViaPrivateKey() {
    const encrypted_obj = EthCrypto.cipher.parse(<ENCRYPTED_TEXT>);
    const decrypted_string = await EthCrypto.decryptWithPrivateKey(<PRIVATE_KEY>, encrypted_obj);
  • 1
    You can't access the private key through MetaMask (without manually exporting it), so this won't work.
    – Morten
    Sep 22, 2020 at 16:04
  • How will the user decrypt the encrypted text, Can we ask them to enter private key for the purpose of decryption, or is there any possibility? Sep 23, 2020 at 9:56
  • Like @Morten mentioned above, MetaMask does not allow programmatically to access private keys due to security reasons. So you can ask users to enter manually their private keys in order to decrypt. Sep 23, 2020 at 10:22
  • 1
    From the technical point of view it is possible to access to the private key stored in metamask (see metamask.zendesk.com/hc/en-us/articles/…). But you have to motivate your users to enter their Metamask password into your page - I would never do this. Feb 28, 2021 at 0:24

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.