[SOLVED] How do I fix this “Bad MAC” error from EthCrypto.js?

Issue

This Content is from Stack Overflow. Question asked by Boris K

I am trying to encrypt and decrypt messages with EthCrypto.js.

Encryption works fine:

        const publicKey = EthCrypto.recoverPublicKey(
            signature,
            EthCrypto.hash.keccak256('Signature verification for video file encryption')
        );
        
        const encrypted = await EthCrypto.encryptWithPublicKey(publicKey, plaintext);
        return EthCrypto.cipher.stringify(encrypted);

I get back an object which looks right:

{
  "iv": "89f37bdafdcb156603804be2bbc4acb3",
  "ephemPublicKey": "04a0e2b193a5e00442f72c69ca035e9f29d9d98e719b8e86d172f14dae1912e443bb35333a776a43b63d6b87108e1f0b1bf0fcdab0e47a7669da9d80b7d7e644ba",
  "ciphertext": "46d5b09c5aad80cbe9e264133afa13e0",
  "mac": "c8237beb6bcee4f5afd361ce0d20364e4bfa533cb530fd1c3821c6e220b8bf7a"
}

then stringify it: const encrypted = await EthCrypto.encryptWithPublicKey(publicKey, plaintext);

The resulting string destringifies into the original object just fine: const parsed = EthCrypto.cipher.parse(ciphertext);

But when I try to decrypt with the private key const decrypted = await EthCrypto.decryptWithPrivateKey(privateKey,parsed); I get a Bad MAC error.

Why is this? It seems like a bug. How do I fix it?



Solution

Solved here:

https://ethereum.stackexchange.com/questions/135888/how-do-i-create-a-signature-with-web3-js-which-ethcrypto-can-process-properly/135900?noredirect=1#comment156154_135900

The issue is that web3.eth.personal.sign() appends additional characters to the string. Therefore, recovering the public key or address from the signature needs to be done thusly:

const address2 = EthCrypto.recover( signature, EthCrypto.hash.keccak256( "\x19Ethereum Signed Message:\n" + message.length + message ) );


This Question was asked in StackOverflow by Boris K and Answered by Boris K It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?