// regression test for 4865198 privatestaticvoid testInvalidSignature(KeyPair kp1, KeyPair kp2) throws Exception {
System.out.println("Testing signature with incorrect key...");
Signature sig = Signature.getInstance("MD5withRSA", provider);
sig.initSign(kp1.getPrivate()); byte[] data = newbyte[100];
sig.update(data); byte[] signature = sig.sign();
sig.initVerify(kp1.getPublic());
sig.update(data); if (sig.verify(signature) == false) { thrownew Exception("verification failed");
}
sig.initVerify(kp2.getPublic());
sig.update(data); // verify needs to return false and not throw an Exception try { if (sig.verify(signature)) { thrownew Exception("verification unexpectedly succeeded");
}
} catch (SignatureException se) { // Yet another kind of failure, OK.
}
}
publicstaticvoid main(String[] args) throws Exception { long start = System.currentTimeMillis();
provider = Security.getProvider("SunRsaSign");
data = newbyte[2048]; // keypair generation is very slow, test only a few short keys int[] keyLengths = {512, 512, 1024};
BigInteger[] pubExps = {null, BigInteger.valueOf(3), null};
KeyPair[] keyPairs = new KeyPair[3]; new Random().nextBytes(data);
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", provider); for (int i = 0; i < keyLengths.length; i++) { int len = keyLengths[i];
BigInteger exp = pubExps[i];
System.out.println("Generating " + len + " bit keypair..."); if (exp == null) {
kpg.initialize(len);
} else {
kpg.initialize(new RSAKeyGenParameterSpec(len, exp));
}
KeyPair kp = kpg.generateKeyPair();
keyPairs[i] = kp;
RSAPublicKey publicKey = (RSAPublicKey)kp.getPublic();
System.out.println(publicKey);
RSAPrivateCrtKey privateKey = (RSAPrivateCrtKey)kp.getPrivate(); if (publicKey.getModulus().equals(privateKey.getModulus()) == false) { thrownew Exception("Moduli do not match");
} if (publicKey.getPublicExponent().equals(privateKey.getPublicExponent()) == false) { thrownew Exception("Exponents do not match");
} int keyLen = publicKey.getModulus().bitLength(); if ((keyLen > len) || (keyLen < len - 1)) { thrownew Exception("Incorrect key length: " + keyLen);
} if (exp != null) { if (exp.equals(publicKey.getPublicExponent()) == false) { thrownew Exception("Incorrect exponent");
}
}
test(privateKey, publicKey);
}
testInvalidSignature(keyPairs[0], keyPairs[1]);
testInvalidSignature(keyPairs[0], keyPairs[2]);
testInvalidSignature(keyPairs[2], keyPairs[0]); long stop = System.currentTimeMillis();
System.out.println("All tests passed (" + (stop - start) + " ms).");
}
}
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.13Bemerkung:
(vorverarbeitet am 2026-06-10)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.