Validate if RSA key matches X.509 certificate in Java -


I have an RSA key and an X.50 9 certificate that I use for SSL connections.

The key and certificate is stored in files in the PEM format (generated by OpenSSL) and is used in Apache HTTP Server environment. Of

Whether using Java security and / or Bonsekastl library methods for an easy way to Key mail example to validate Java code (Autaksel to parse and output without executing the binary) Using certificate?

The following code compares SHA-1 to the modulus within the public and private key. (Unless you have broken the key pair generation system or random generator at all).

Note that the key to keeping the following code in unencrypted PKCS # 8 format is required. Instead of using PKCS # 12 and keystore (providing password), it may be better to load the binary PKCS # 12 file.

  openssl pkcs8 -topk8 -in key.pem -out keypk8 Pem -nocrypt   

and finally the Java code:

 < Code> import static org.bouncycastle.util.encoders.Hex.toHexString; Import java.io.biteArrayInputStream; Import java.io.fileReader; Import java.security.KeyFactory; Import java.security.MessageDigest; Import java.security.PrivateKey; Import java.security.PublicKey; Import java.security.cert.ertificate; Import java.security.cert.CertificateFactory; Import java.security.cert.X509 certificate; Import java.security.interfaces.RSAPrivateKey; Import java.security.interfaces.RSAPublicKey; Import java.security.spec.KeySpec; Import java.security.spec.PKCS8EncodedKeySpec; Import org.bouncycastle.util.io.pem.PemObject; Import org.bouncycastle.util.io.pem.PemReader; Compare Public SectorCricket Endk. Checks whether the certificate and the RSA private key match * * @ Param in the AGR [0] and RG [1] * the argument of the public key certificate file / Public static zero main (string [] args) {final permarorder certificate reader = new pyramer (new) FileReader (args [0])); Last PemObject certAsPemObject = certReader.readPemObject (); [If (! CertAsPemObject.getType (). Equals ignore currencies ( "Certificate")) is no certificate (strike out illegal execution conclusion ( "Certificate file, but a" + certAsPemObject.getType ());} final byte] x509 data = Srtspemobjektkget content (); letter final certificate fact fact = Srtifiketfain. gate instance ( "x 50 9"); final certificate cert = fact.generate certificate (new Baitareinputstrym (X509 data)); if (! (X509 certificates certificate )) (Strike out illegal execution conclusion ( "Certificate file does not form an X509 certificate");} Last Sarwajnikki Sarwajnikke = cert.getPublicKey (); if (! (Public key presentation RSAPublicKey)) (new illegal execution conclude ( "The certificate file does not contain an RSA public key, but a" + publicKey.getClass (). GetName ())}} Last RSAPublicKey rsaPublicKey = publicKey; Last byte [] certModulusData = rsaPublicKey.getModulus (). ToByteArray ); Last messageDigestSha1 = Message Dezzis Tkget Instens ( "SHA-1"); final byte [] certID = sha1.digest (certModulusData); Final string certIDinHex = toHexString (certID); Last PemReader keyReader = new PemReader (new FileReader (Args [1])); Last PemObject keyAsPemObject = keyReader.readPemObject (); If (! KeyAsPemObject.getType (). Equals ignoring currencies ( "private key")) (strike out illegal execution conclude a private key ( "key file, but a" + keyAsPemObject.getType ());} final byte [] PersonalcardData = KAPSObject.gate content (); Last keyfire keyfat = keyfighter .Get instance ("RSA"); Last KeySPEC keySPEC = New PKCS 8 EncodedKSSPEC (PersonalizedData); Last private key = keyFact.generatePrivate (keySpec); if (! (private key example RSAPrivateKey)) (new invalid records throw exceptions ("Key file does not have an X509 encoded private key");} Last RSAPrivateKey rsaPrivateKey = privateKey; Last byte [] keyModulusData = rsaPrivateKey.getModulus (). ToByteArray (); Last Byte [] keyID = sha1.digest (keyModulusData); Last string keyIDinHex = toHexString (keyID); System.out.println (args [0] + " : "+ certIDinHex); System.out.println (Arges [1] +": "+ keyIDinHex); if (certIDinHex.equalsIgnoreCase (keyIDinHex)) {System.out.println (" matching "); System.exit (0); } Else {System.out.println ("no matches"); System.exit (-1); }} Hold (exception e) {e.printStackTrace (System.err); System.exit (-2); }}}    

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -