When I load the JSON file of the wallet into MyEtherWallet, how does it determine if the passphrase is correct or incorrect? After I initially give a passphrase to encrypt my wallet's private key, is the mapping between the decrypted and encrypted private keys stored anywhere?
I don't know how MyEtherWallet does it, but this way seems logical: The keystore contains the address belonging to the encrypted private key. After you received the private key as a result of the decryption, you can derive a public key and finally an address of it. You can compare the resulting address with the address stored within the keystore to proof if the decrypted private key is correct.
I have looked into the structure of a keystore file and saw that a message authentication code (MAC) is specified. You can compare the mac given with the mac you derived to proof the decryptions validity. By looking into the pyethereum code, I have found the following lines of code which do this:
mac1 = sha3(derivedkey[16:32] + ctext) mac2 = decode_hex(cryptdata["mac"]) if mac1 != mac2: raise ValueError("MAC mismatch. Password incorrect?")