Class PrivateKeyReader

java.lang.Object
org.omegazero.net.util.PrivateKeyReader

public final class PrivateKeyReader extends Object
Class for reading a RSA or EC private key from a PEM-formatted string.

It can read PEM files with PKCS#8 or PKCS#1 encodings. It does not support encrypted PEM files.

This implementation is from here (archived page). For more information and a link to the original source code, see the license header of this source file.

  • Field Details

  • Method Details

    • read

      public static PrivateKey read(String keyString) throws IOException, GeneralSecurityException
      Parses the given PEM-formatted string representing a private key.
      Returns:
      The PrivateKey
      Throws:
      IOException - If the given key string is malformed
      GeneralSecurityException - If the given key string could not be parsed
    • getRSAKeySpec

      public static RSAPrivateCrtKeySpec getRSAKeySpec(byte[] keyBytes) throws IOException
      Convert PKCS#1 encoded private key into RSAPrivateCrtKeySpec.

      The ASN.1 syntax for the private key with CRT is

       -- 
       -- Representation of RSA private key with information for the CRT algorithm.
       --
       RSAPrivateKey ::= SEQUENCE {
         version           Version, 
         modulus           INTEGER,  -- n
         publicExponent    INTEGER,  -- e
         privateExponent   INTEGER,  -- d
         prime1            INTEGER,  -- p
         prime2            INTEGER,  -- q
         exponent1         INTEGER,  -- d mod (p-1)
         exponent2         INTEGER,  -- d mod (q-1) 
         coefficient       INTEGER,  -- (inverse of q) mod p
         otherPrimeInfos   OtherPrimeInfos OPTIONAL 
       }
       
      Parameters:
      keyBytes - PKCS#1 encoded key
      Returns:
      KeySpec
      Throws:
      IOException