public final class KeyPairUtil extends Object
Modifier and Type | Method and Description |
---|---|
static PrivateKey |
decodePrivateKey(byte[] encodedKey)
Decodes an encoded private key in either PKCS#8 or OpenSSL "traditional" format in either DER or PEM encoding.
|
static PrivateKey |
decodePrivateKey(byte[] encryptedKey,
char[] password)
Decodes an encrypted private key.
|
static PublicKey |
decodePublicKey(byte[] encoded)
Decodes public keys formatted in an X.509 SubjectPublicKeyInfo structure in either PEM or DER encoding.
|
static boolean |
isKeyPair(DSAPublicKey pubKey,
DSAPrivateKey privKey)
Determines whether the given DSA public and private keys form a proper key pair by computing and verifying a
digital signature with the keys.
|
static boolean |
isKeyPair(ECPublicKey pubKey,
ECPrivateKey privKey)
Determines whether the given EC public and private keys form a proper key pair by computing and verifying a digital
signature with the keys.
|
static boolean |
isKeyPair(PublicKey pubKey,
PrivateKey privKey)
Determines whether the given public and private keys form a proper key pair by computing and verifying a digital
signature with the keys.
|
static boolean |
isKeyPair(RSAPublicKey pubKey,
RSAPrivateKey privKey)
Determines whether the given RSA public and private keys form a proper key pair by computing and verifying a
digital signature with the keys.
|
static int |
length(PrivateKey privKey)
Gets the length in bits of a private key where key size is dependent on the particulars of the algorithm.
|
static int |
length(PublicKey pubKey)
Gets the length in bits of a public key where key size is dependent on the particulars of the algorithm.
|
static PrivateKey |
readPrivateKey(File file)
Reads an encoded private key from a file.
|
static PrivateKey |
readPrivateKey(File file,
char[] password)
Reads an encrypted private key from a file.
|
static PrivateKey |
readPrivateKey(InputStream in)
Reads an encoded private key from an input stream.
|
static PrivateKey |
readPrivateKey(InputStream in,
char[] password)
Reads an encrypted private key from an input stream.
|
static PrivateKey |
readPrivateKey(String path)
Reads an encoded private key from a file at the given path.
|
static PrivateKey |
readPrivateKey(String path,
char[] password)
Reads an encrypted private key from a file at the given path.
|
static PublicKey |
readPublicKey(File file)
Reads a DER or PEM-encoded public key from a file.
|
static PublicKey |
readPublicKey(InputStream in)
Reads a DER or PEM-encoded public key from data in the given stream.
|
static PublicKey |
readPublicKey(String path)
Reads a DER or PEM-encoded public key from a file.
|
public static int length(PublicKey pubKey)
pubKey
- Public key.public static int length(PrivateKey privKey)
privKey
- Private key.public static boolean isKeyPair(PublicKey pubKey, PrivateKey privKey) throws CryptoException
pubKey
- DSA, RSA or EC public key.privKey
- DSA, RSA, or EC private key.CryptoException
- on key validation errors.public static boolean isKeyPair(DSAPublicKey pubKey, DSAPrivateKey privKey) throws CryptoException
pubKey
- DSA public key.privKey
- DSA private key.CryptoException
- on key validation errors.public static boolean isKeyPair(RSAPublicKey pubKey, RSAPrivateKey privKey) throws CryptoException
pubKey
- RSA public key.privKey
- RSA private key.CryptoException
- on key validation errors.public static boolean isKeyPair(ECPublicKey pubKey, ECPrivateKey privKey) throws CryptoException
pubKey
- EC public key.privKey
- EC private key.CryptoException
- on key validation errors.public static PrivateKey readPrivateKey(String path) throws EncodingException, StreamException
decodePrivateKey(byte[])
for supported asymmetric algorithms.path
- Path to private key file.EncodingException
- on key encoding errors.StreamException
- on IO errors reading data from file.public static PrivateKey readPrivateKey(File file) throws EncodingException, StreamException
decodePrivateKey(byte[])
for supported asymmetric algorithms.file
- Private key file.EncodingException
- on key encoding errors.StreamException
- on IO errors reading data from file.public static PrivateKey readPrivateKey(InputStream in) throws EncodingException, StreamException
decodePrivateKey(byte[])
for supported asymmetric algorithms. The InputStream
parameter is closed by this method.in
- Input stream containing private key data.EncodingException
- on key encoding errors.StreamException
- on IO errors reading data from file.public static PrivateKey readPrivateKey(String path, char[] password) throws EncodingException, StreamException
decodePrivateKey(byte[])
for supported asymmetric algorithms.path
- Path to private key file.password
- Password used to encrypt private key.EncodingException
- on key encoding errors.StreamException
- on IO errors.public static PrivateKey readPrivateKey(File file, char[] password) throws EncodingException, StreamException
decodePrivateKey(byte[])
for supported asymmetric algorithms.file
- Private key file.password
- Password used to encrypt private key.EncodingException
- on key encoding errors.StreamException
- on IO errors.public static PrivateKey readPrivateKey(InputStream in, char[] password) throws EncodingException, StreamException
decodePrivateKey(byte[])
for supported asymmetric algorithms. The InputStream
parameter is closed by this method.in
- Input stream containing private key data.password
- Password used to encrypt private key.EncodingException
- on key encoding errors.StreamException
- on IO errors.public static PrivateKey decodePrivateKey(byte[] encodedKey) throws EncodingException
encodedKey
- Encoded private key data.EncodingException
- on key encoding errors.public static PrivateKey decodePrivateKey(byte[] encryptedKey, char[] password) throws EncodingException
Keys from the following asymmetric algorithms are supported:
encryptedKey
- Encrypted private key data.password
- Password used to encrypt private key.EncodingException
- on key encoding errors.public static PublicKey readPublicKey(String path) throws EncodingException, StreamException
path
- Path to DER or PEM-encoded public key file.EncodingException
- on key encoding errors.StreamException
- on IO errors.public static PublicKey readPublicKey(File file) throws EncodingException, StreamException
file
- DER or PEM-encoded public key file.EncodingException
- on key encoding errors.StreamException
- on IO errors.public static PublicKey readPublicKey(InputStream in) throws EncodingException, StreamException
InputStream
parameter is closed by
this method.in
- Input stream containing an encoded key.EncodingException
- on key encoding errors.StreamException
- on IO errors.public static PublicKey decodePublicKey(byte[] encoded) throws EncodingException
encoded
- Encoded public key bytes.EncodingException
- on key encoding errors.Copyright © 2003-2024 Virginia Tech. All Rights Reserved.