[Ethereum] Can we decrypt a message by using private key through Metamask

encryptionpublic-keyreactsolidity

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?

Best Answer

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);
}
Related Topic