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 Summary
Fields Modifier and Type Field Description static List<String>AVAILABLE_EC_CURVES
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static PublicKeyderivePublicKey(PrivateKey privateKey)Derives the public key from a private key.static voidexportToPEM(PublicKey publicKey, OutputStream pkOutputStream)Exports a PublicKey to an OutputStream in PEM format.static voidexportToPKCS8(PrivateKey privateKey, OutputStream pkOutputStream, char[] password)Exports a PrivateKey to an OutputStream in PKCS8 format.static voidexportToPKCS8PEM(PrivateKey privateKey, OutputStream pkOutputStream)Exports a PrivateKey to an OutputStream in PKCS8 PEM format.static voidexportToPKCS8PEM(PrivateKey privateKey, OutputStream pkOutputStream, char[] password)Exports a PrivateKey to an OutputStream in PKCS8 PEM format.static KeyPairgenerateDSAKey(int keySize)Generates a new DSA key pair with the specified key size.static KeyPairgenerateECKey(int keySize)Generates a new EC key pair with the specified curve.static KeyPairgenerateECKey(String curve)Generates a new EC key pair with the specified curve.static KeyPairgenerateRSAKey(int keySize)Generates a new RSA key pair with the specified key size.static intgetKeySize(PublicKey publicKey)Get the key size in bits for a public key.static PrivateKeyimportPrivateKey(InputStream pkInputStream, char[] password)Imports a private key, handling both encrypted and unencrypted formats.static PrivateKeyloadPrivateKey(byte[] privateKeyBytes, String algorithm)Creates aPrivateKeyfrom encoded key bytesstatic PublicKeyloadPublicKey(byte[] publicKeyBytes, String algorithm)Creates aPublicKeyfrom encoded key bytes-
Methods inherited from class de.xima.fc.cryptography.BouncyCastleSupport
buildSignedCertificate, createCMSContentEncryptorBuilder, createContentSigner, createJceKeyTransRecipientInfoGenerator, createKeyFactory, createKeyPairGenerator, createKeyStore, createOpenSSLPKCS8DecryptorProviderBuilder, createPEMKeyConverter, createSignature, createX509CertificateConverter, createX509CertificateFactory
-
-
-
-
Method Detail
-
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 exportpkOutputStream- The output stream to write the private key data topassword- The password to protect the private key, or null for unencrypted- Throws:
FcCertificateManagementException- If an error occurs during private key exportIOException
-
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 occursFcCertificateManagementException- 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 exportpassword- The password to protect the private key, or null for unencrypted- Throws:
IOException- If an I/O error occursFcCertificateManagementException- 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 keypassword- 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 aPrivateKeyfrom encoded key bytes- Parameters:
privateKeyBytes- Encoded private key bytesalgorithm- 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 aPublicKeyfrom encoded key bytes- Parameters:
publicKeyBytes- Encoded public key bytesalgorithm- Key algorithm (e.g. "RSA", "DSA", "EC")- Returns:
- PublicKey object
- Throws:
FcCertificateManagementException- If any error occurs during key creation
-
-