Enum PEM

  • All Implemented Interfaces:
    java.io.Serializable, java.lang.Comparable<PEM>

    public enum PEM
    extends java.lang.Enum<PEM>
    A PEM utility that can be used to read keys from PEM. With this PEM utility, private keys in either PKCS#1 or PKCS#8 PEM encoded format can be read without the need to depend on the Bouncy Castle library.

    Some background information:

    • Interestingly, the creation of a CloudFront Key Pair via the AWS console would result in a private key in PKCS#1 PEM format.
    • Unfortunately, the JDK doesn't provide a means to load PEM key encoded in PKCS#1 without adding the Bouncy Castle to the classpath. The JDK can only load PEM key encoded in PKCS#8 encoding.
    • One the other hand, one can use openssl to convert a PEM file from PKCS#1 to PKCS#8. Example:
       openssl pkcs8 -topk8 -in pk-APKAJM22QV32R3I2XVIQ.pem -inform pem -out pk-APKAJM22QV32R3I2XVIQ_pk8.pem  -outform pem -nocrypt
       
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.security.PrivateKey readPrivateKey​(java.io.InputStream is)
      Returns the first private key that is found from the input stream of a PEM file.
      static java.security.PublicKey readPublicKey​(java.io.InputStream is)
      Returns the first public key that is found from the input stream of a PEM file.
      static PEM valueOf​(java.lang.String name)
      Returns the enum constant of this type with the specified name.
      static PEM[] values()
      Returns an array containing the constants of this enum type, in the order they are declared.
      • Methods inherited from class java.lang.Enum

        clone, compareTo, equals, finalize, getDeclaringClass, hashCode, name, ordinal, toString, valueOf
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, wait, wait, wait
    • Method Detail

      • values

        public static PEM[] values()
        Returns an array containing the constants of this enum type, in the order they are declared. This method may be used to iterate over the constants as follows:
        for (PEM c : PEM.values())
            System.out.println(c);
        
        Returns:
        an array containing the constants of this enum type, in the order they are declared
      • valueOf

        public static PEM valueOf​(java.lang.String name)
        Returns the enum constant of this type with the specified name. The string must match exactly an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)
        Parameters:
        name - the name of the enum constant to be returned.
        Returns:
        the enum constant with the specified name
        Throws:
        java.lang.IllegalArgumentException - if this enum type has no constant with the specified name
        java.lang.NullPointerException - if the argument is null
      • readPrivateKey

        public static java.security.PrivateKey readPrivateKey​(java.io.InputStream is)
                                                       throws java.security.spec.InvalidKeySpecException,
                                                              java.io.IOException
        Returns the first private key that is found from the input stream of a PEM file.
        Parameters:
        is - Inputstream to read a private key from
        Returns:
        the first PrivateKey found in the stream
        Throws:
        java.security.spec.InvalidKeySpecException - if failed to convert the DER bytes into a private key.
        java.lang.IllegalArgumentException - if no private key is found.
        java.io.IOException - if an IO exception occurs while reading the stream
      • readPublicKey

        public static java.security.PublicKey readPublicKey​(java.io.InputStream is)
                                                     throws java.security.spec.InvalidKeySpecException,
                                                            java.io.IOException
        Returns the first public key that is found from the input stream of a PEM file.
        Parameters:
        is - The Input stream to read
        Returns:
        the first PublicKey found in the stream
        Throws:
        java.security.spec.InvalidKeySpecException - if failed to convert the DER bytes into a public key.
        java.lang.IllegalArgumentException - if no public key is found.
        java.io.IOException - if an IO exception occurs while reading the stream