@Test
public void testBitFlipAgainstSignatureInstance() 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();
  // Sign with EcdsaSign.
  String message = "Hello";
  EcdsaSignJce signer = new EcdsaSignJce(priv, HashType.SHA256, EcdsaEncoding.DER);
  byte[] signature = signer.sign(message.getBytes("UTF-8"));
  for (int i = 0; i < signature.length; i++) {
    for (int j = 0; j < 8; j++) {
      signature[i] = (byte) (signature[i] ^ (1 << j));
      // Verify with JCE's Signature.
      Signature verifier = Signature.getInstance("SHA256WithECDSA");
      verifier.initVerify(pub);
      verifier.update(message.getBytes("UTF-8"));
      boolean verified = true;
      try {
        verified = verifier.verify(signature);
      } catch (GeneralSecurityException expected) {
        verified = false;
      }
      assertFalse(verified);
      signature[i] = (byte) (signature[i] ^ (1 << j));
    }
  }
}