chris01
chris01

Reputation: 12431

How to check if generated keypair matches?

Is there a way I can check if a private-key matches a public-key with Java JCE?

    // generate a keypair
    KeyPairGenerator kpg = KeyPairGenerator.getInstance ("RSA", provname);
    kpg.initialize (4096);
    KeyPair kp = kpg.generateKeyPair ();

    // does kp.getPublic () matches kp.getPrivate ()???

Best I can think of is to use it.

    // sign and verify
    Signature sign = Signature.getInstance ("SHA256withRSA", "BC");
    sign.initSign (kp.getPrivate ());
    String str ="text";
    sign.update (str.getBytes());
    byte sv [] = sign.sign ();  
    sign.initVerify(kp.getPublic());
    sign.update (str.getBytes ());
    boolean x=sign.verify (sv);       // must be true if keys match!

But maybe there is another easier way??

Upvotes: 0

Views: 74

Answers (0)

Related Questions