Class KeyPairUtil

java.lang.Object
de.xima.fc.cryptography.BouncyCastleSupport
de.xima.fc.certificate.ms.impl.util.KeyPairUtil

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

    • AVAILABLE_EC_CURVES

      public static final List<String> AVAILABLE_EC_CURVES
  • Method Details

    • derivePublicKey

      public static PublicKey derivePublicKey(PrivateKey privateKey) throws FcCertificateManagementException
      Derives the public key from a private key.
      Parameters:
      privateKey - Private key
      Returns:
      PublicKey objecty
      Throws:
      FcCertificateManagementException
    • 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
    • exportToPKCS8

      public static void exportToPKCS8(PrivateKey privateKey, OutputStream pkOutputStream, char[] password) throws IOException, FcCertificateManagementException
      Exports a PrivateKey to an OutputStream in PKCS8 format.
      Parameters:
      privateKey - The private key to export
      pkOutputStream - The output stream to write the private key data to
      password - The password to protect the private key, or null for unencrypted
      Throws:
      FcCertificateManagementException - If an error occurs during private key export
      IOException
    • exportToPKCS8PEM

      public static void exportToPKCS8PEM(PrivateKey privateKey, OutputStream pkOutputStream) throws IOException, FcCertificateManagementException
      Exports a PrivateKey to an OutputStream in PKCS8 PEM format.
      Parameters:
      privateKey - The private key to export
      Throws:
      IOException - If an I/O error occurs
      FcCertificateManagementException - If there's an error creating the encryptor or any other encryption-related error
    • 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
    • generateECKey

      public static KeyPair generateECKey(int keySize) throws FcCertificateManagementException
      Generates a new EC key pair with the specified curve.
      Parameters:
      keySize - Key size in bits
      Returns:
      KeyPair object containing the generated key pair
      Throws:
      FcCertificateManagementException - If any error occurs during key generation
    • generateECKey

      public static KeyPair generateECKey(String curve) throws FcCertificateManagementException
      Generates a new EC key pair with the specified curve.
      Parameters:
      curve - Curve name
      Returns:
      KeyPair object containing the generated key pair
      Throws:
      FcCertificateManagementException - If any error occurs during key generation
    • 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
    • loadPrivateKey

      public static PrivateKey loadPrivateKey(byte[] privateKeyBytes, String algorithm) throws FcCertificateManagementException
      Creates a PrivateKey from encoded key bytes
      Parameters:
      privateKeyBytes - Encoded private key bytes
      algorithm - Key algorithm (e.g. "RSA", "DSA", "EC")
      Returns:
      PrivateKey object
      Throws:
      FcCertificateManagementException - If any error occurs during key creation
    • loadPublicKey

      public static PublicKey loadPublicKey(byte[] publicKeyBytes, String algorithm) throws FcCertificateManagementException
      Creates a PublicKey from encoded key bytes
      Parameters:
      publicKeyBytes - Encoded public key bytes
      algorithm - Key algorithm (e.g. "RSA", "DSA", "EC")
      Returns:
      PublicKey object
      Throws:
      FcCertificateManagementException - If any error occurs during key creation