KeyFactory kf = KeyFactory.getInstance(kpgAlgo, provider); // For each public KeySpec supported by KeyPairGenerator for (Class pubSpecType
: getCompatibleKeySpecs(kpgAlgo, KeyType.PUBLIC)) { //For each private KeySpec supported by KeyPairGenerator for (Class priSpecType
: getCompatibleKeySpecs(kpgAlgo, KeyType.PRIVATE)) { // Transform original PublicKey through KeySpec
KeySpec pubSpec = kf.getKeySpec(kp.getPublic(), pubSpecType);
PublicKey pubKey = kf.generatePublic(pubSpec); // Transform original PrivateKey through KeySpec
KeySpec priSpec = kf.getKeySpec(kp.getPrivate(), priSpecType);
PrivateKey priKey = kf.generatePrivate(priSpec);
// Test the keys are equal after transformation through KeySpec.
testKeyEquals(kp, pubKey, priKey); // Test the keys are serializable.
testSerialize(kp); // Test Parameter name. if (!kaAlgo.equals("DiffieHellman")) {
testParamName(priSpec, pubSpec);
} // Compare KeyAgreement secret generated from original keys // and by the keys after transformed through KeySpec. if (!Arrays.equals(getKeyAgreementSecret(provider, kaAlgo,
kp.getPublic(), kp.getPrivate()),
getKeyAgreementSecret(provider, kaAlgo,
pubKey, priKey))) { thrownew RuntimeException("KeyAgreement secret mismatch.");
}
}
}
}
X509EncodedKeySpec pubSpec
= new X509EncodedKeySpec(kp.getPublic().getEncoded());
PublicKey pubKey = kf.generatePublic(pubSpec);
// Test the keys are equal after transformation through KeySpec.
testKeyEquals(kp, pubKey, priKey); // Test the keys are serializable.
testSerialize(kp); // Test Parameter name. if (!kaAlgo.equals("DiffieHellman")) {
testParamName(priSpec, pubSpec);
} // Compare KeyAgreement secret generated from original keys // and by the keys after transformed through KeySpec. if (!Arrays.equals(getKeyAgreementSecret(provider, kaAlgo,
kp.getPublic(), kp.getPrivate()),
getKeyAgreementSecret(provider, kaAlgo, pubKey, priKey))) { thrownew RuntimeException("KeyAgreement secret mismatch.");
}
}
if (!kp.getPrivate().equals(priKey)
&& kp.getPrivate().hashCode() != priKey.hashCode()) { thrownew RuntimeException("PrivateKey is not equal with PrivateKey"
+ " generated through KeySpec");
} if (!kp.getPublic().equals(pubKey)
&& kp.getPublic().hashCode() != pubKey.hashCode()) { thrownew RuntimeException("PublicKey is not equal with PublicKey"
+ " generated through KeySpec");
}
}
// Verify Serialized PrivateKey instance. if (!keyPair.getPrivate().equals(
deserializedCopy(keyPair.getPrivate(), PrivateKey.class))) { thrownew RuntimeException("PrivateKey is not equal with PrivateKey"
+ " generated through Serialization");
} // Verify Serialized PublicKey instance. if (!keyPair.getPublic().equals(
deserializedCopy(keyPair.getPublic(), PublicKey.class))) { thrownew RuntimeException("PublicKey is not equal with PublicKey"
+ " generated through Serialization");
} // Verify Serialized KeyPair instance.
KeyPair copy = deserializedCopy(keyPair, KeyPair.class); if (!keyPair.getPrivate().equals(copy.getPrivate())) { thrownew RuntimeException("PrivateKey is not equal with PrivateKey"
+ " generated through Serialized KeyPair");
} if (!keyPair.getPublic().equals(copy.getPublic())) { thrownew RuntimeException("PublicKey is not equal with PublicKey"
+ " generated through Serialized KeyPair");
}
}
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.