public final class CertAndKeyGen
extends java.lang.Object
This provides some simple certificate management functionality. Specifically, it allows you to create self-signed X.509 certificates as well as PKCS 10 based certificate signing requests.
Keys for some public key signature algorithms have algorithm parameters, such as DSS/DSA. Some sites' Certificate Authorities adopt fixed algorithm parameters, which speeds up some operations including key generation and signing. At this time, this interface does not provide a way to provide such algorithm parameters, e.g. by providing the CA certificate which includes those parameters.
Also, note that at this time only signature-capable keys may be acquired through this interface. Diffie-Hellman keys, used for secure key exchange, may be supported later.
PKCS10,
X509CertImpl| Constructor and Description |
|---|
CertAndKeyGen(java.lang.String keyType,
java.lang.String sigAlg)
Creates a CertAndKeyGen object for a particular key type
and signature algorithm.
|
CertAndKeyGen(java.lang.String keyType,
java.lang.String sigAlg,
java.lang.String providerName)
Creates a CertAndKeyGen object for a particular key type,
signature algorithm, and provider.
|
| Modifier and Type | Method and Description |
|---|---|
void |
generate(int keyBits)
Generates a random public/private key pair, with a given key
size.
|
java.security.PrivateKey |
getPrivateKey()
Returns the private key of the generated key pair.
|
X509Key |
getPublicKey()
Returns the public key of the generated key pair if it is of type
X509Key, or null if the public key is of a different type. |
X509Cert |
getSelfCert(X500Name myname,
long validity)
|
java.security.cert.X509Certificate |
getSelfCertificate(X500Name myname,
java.util.Date firstDate,
long validity)
Returns a self-signed X.509v3 certificate for the public key.
|
java.security.cert.X509Certificate |
getSelfCertificate(X500Name myname,
long validity) |
void |
setRandom(java.security.SecureRandom generator)
Sets the source of random numbers used when generating keys.
|
public CertAndKeyGen(java.lang.String keyType,
java.lang.String sigAlg)
throws java.security.NoSuchAlgorithmException
keyType - type of key, e.g. "RSA", "DSA"sigAlg - name of the signature algorithm, e.g. "MD5WithRSA",
"MD2WithRSA", "SHAwithDSA".java.security.NoSuchAlgorithmException - on unrecognized algorithms.public CertAndKeyGen(java.lang.String keyType,
java.lang.String sigAlg,
java.lang.String providerName)
throws java.security.NoSuchAlgorithmException,
java.security.NoSuchProviderException
keyType - type of key, e.g. "RSA", "DSA"sigAlg - name of the signature algorithm, e.g. "MD5WithRSA",
"MD2WithRSA", "SHAwithDSA".providerName - name of the providerjava.security.NoSuchAlgorithmException - on unrecognized algorithms.java.security.NoSuchProviderException - on unrecognized providers.public void setRandom(java.security.SecureRandom generator)
public void generate(int keyBits)
throws java.security.InvalidKeyException
Note that not all values of "keyBits" are valid for all algorithms, and not all public key algorithms are currently supported for use in X.509 certificates. If the algorithm you specified does not produce X.509 compatible keys, an invalid key exception is thrown.
keyBits - the number of bits in the keys.java.security.InvalidKeyException - if the environment does not
provide X.509 public keys for this signature algorithm.public X509Key getPublicKey()
X509Key, or null if the public key is of a different type.
XXX Note: This behaviour is needed for backwards compatibility.
What this method really should return is the public key of the
generated key pair, regardless of whether or not it is an instance of
X509Key. Accordingly, the return type of this method
should be PublicKey.public java.security.PrivateKey getPrivateKey()
Be extremely careful when handling private keys. When private keys are not kept secret, they lose their ability to securely authenticate specific entities ... that is a huge security risk!
@Deprecated public X509Cert getSelfCert(X500Name myname, long validity) throws java.security.InvalidKeyException, java.security.SignatureException, java.security.NoSuchAlgorithmException
Such certificates normally are used to identify a "Certificate Authority" (CA). Accordingly, they will not always be accepted by other parties. However, such certificates are also useful when you are bootstrapping your security infrastructure, or deploying system prototypes.
myname - X.500 name of the subject (who is also the issuer)validity - how long the certificate should be valid, in secondsjava.security.InvalidKeyExceptionjava.security.SignatureExceptionjava.security.NoSuchAlgorithmExceptionpublic java.security.cert.X509Certificate getSelfCertificate(X500Name myname, java.util.Date firstDate, long validity) throws java.security.cert.CertificateException, java.security.InvalidKeyException, java.security.SignatureException, java.security.NoSuchAlgorithmException, java.security.NoSuchProviderException
Such certificates normally are used to identify a "Certificate Authority" (CA). Accordingly, they will not always be accepted by other parties. However, such certificates are also useful when you are bootstrapping your security infrastructure, or deploying system prototypes.
myname - X.500 name of the subject (who is also the issuer)firstDate - the issue time of the certificatevalidity - how long the certificate should be valid, in secondsjava.security.cert.CertificateException - on certificate handling errors.java.security.InvalidKeyException - on key handling errors.java.security.SignatureException - on signature handling errors.java.security.NoSuchAlgorithmException - on unrecognized algorithms.java.security.NoSuchProviderException - on unrecognized providers.public java.security.cert.X509Certificate getSelfCertificate(X500Name myname, long validity) throws java.security.cert.CertificateException, java.security.InvalidKeyException, java.security.SignatureException, java.security.NoSuchAlgorithmException, java.security.NoSuchProviderException
java.security.cert.CertificateExceptionjava.security.InvalidKeyExceptionjava.security.SignatureExceptionjava.security.NoSuchAlgorithmExceptionjava.security.NoSuchProviderExceptionCopyright © 2020. All Rights Reserved.