@Test
public void testModification() throws Exception {

  ECParameterSpec ecParams = EllipticCurves.getNistP256Params();
  KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
  keyGen.initialize(ecParams);
  KeyPair keyPair = keyGen.generateKeyPair();
  ECPublicKey pub = (ECPublicKey) keyPair.getPublic();
  ECPrivateKey priv = (ECPrivateKey) keyPair.getPrivate();
  EcdsaEncoding[] encodings = new EcdsaEncoding[] {EcdsaEncoding.IEEE_P1363, EcdsaEncoding.DER};
  for (EcdsaEncoding encoding : encodings) {
    // Sign with EcdsaSignJce
    EcdsaSignJce signer = new EcdsaSignJce(priv, HashType.SHA256, encoding);
    byte[] message = "Hello".getBytes("UTF-8");
    byte[] signature = signer.sign(message);
    // Verify with EcdsaVerifyJce.
    EcdsaVerifyJce verifier = new EcdsaVerifyJce(pub, HashType.SHA256, encoding);
    for (BytesMutation mutation : TestUtil.generateMutations(signature)) {
      try {
        verifier.verify(mutation.value, message);
        fail(
            String.format(
                "Invalid signature, should have thrown exception : signature = %s, message = %s, "
                    + " description = %s",
                Hex.encode(mutation.value), Arrays.toString(message), mutation.description));
      } catch (GeneralSecurityException expected) {
        // Expected.
      }
    }
    // Encodings mismatch.
    verifier =
        new EcdsaVerifyJce(
            pub,
            HashType.SHA256,
            encoding == EcdsaEncoding.IEEE_P1363 ? EcdsaEncoding.DER : EcdsaEncoding.IEEE_P1363);
    try {
      verifier.verify(signature, message);
      fail("Invalid signature, should have thrown exception");
    } catch (GeneralSecurityException expected) {
      // Expected.
    }
  }
}