Class KeyPairUtil


  • public final class KeyPairUtil
    extends BouncyCastleSupport
    Utility class for generating, importing, exporting, and deriving key pairs.
    • Field Detail

      • AVAILABLE_EC_CURVES

        public static final List<String> AVAILABLE_EC_CURVES
    • Method Detail

      • exportToPEM

        public static void exportToPEM​(PublicKey publicKey,
                                       OutputStream pkOutputStream)
                                throws IOException
        Exports a PublicKey to an OutputStream in PEM format.
        Parameters:
        publicKey - The public key to export
        Throws:
        IOException - If an I/O error occurs
      • exportToPKCS8PEM

        public static void exportToPKCS8PEM​(PrivateKey privateKey,
                                            OutputStream pkOutputStream,
                                            char[] password)
                                     throws IOException,
                                            FcCertificateManagementException
        Exports a PrivateKey to an OutputStream in PKCS8 PEM format. If password is null or empty, the key is exported unencrypted. Otherwise, the key is encrypted with the provided password.
        Parameters:
        privateKey - The private key to export
        password - The password to protect the private key, or null for unencrypted
        Throws:
        IOException - If an I/O error occurs
        FcCertificateManagementException - If there's an error creating the encryptor or any other encryption-related error
      • generateDSAKey

        public static KeyPair generateDSAKey​(int keySize)
        Generates a new DSA key pair with the specified key size.
        Parameters:
        keySize - Key size in bits
        Returns:
        KeyPair object containing the generated key pair
      • generateRSAKey

        public static KeyPair generateRSAKey​(int keySize)
        Generates a new RSA key pair with the specified key size.
        Parameters:
        keySize - Key size in bits
        Returns:
        KeyPair object containing the generated key pair
      • getKeySize

        public static int getKeySize​(PublicKey publicKey)
        Get the key size in bits for a public key.
        Parameters:
        publicKey - Public key
        Returns:
        Key size in bits
      • importPrivateKey

        public static PrivateKey importPrivateKey​(InputStream pkInputStream,
                                                  char[] password)
                                           throws FcCertificateManagementException
        Imports a private key, handling both encrypted and unencrypted formats.
        Parameters:
        pkInputStream - input stream for the private key
        password - password for encrypted keys (can be null for unencrypted keys)
        Returns:
        PrivateKey object
        Throws:
        FcCertificateManagementException