edu.vt.middleware.crypt.util
Class PemHelper

java.lang.Object
  extended by edu.vt.middleware.crypt.util.PemHelper

public class PemHelper
extends Object

Helper class provides operations for encoding/decoding cryptographic keys and certificates to PEM format.

Version:
$Revision: 2745 $
Author:
Middleware Services

Field Summary
static String DEK_INFO
          Decryption infor tag for PEM-encoded private key in OpenSSL format.
static String FOOTER_END
          PEM encoding footer start string.
static String HEADER_BEGIN
          PEM encoding header start string.
static String KEY_ENCRYPTION_ALGORITHM
          Encryption algorithm used for password-protected private keys.
static String PROC_TYPE
          Procedure type tag for PEM-encoded private key in OpenSSL format.
 
Constructor Summary
protected PemHelper()
          Hidden constructor of utility class.
 
Method Summary
static byte[] decode(byte[] pem)
          Decodes a PEM-encoded cryptographic object into the raw bytes of its ASN.1 encoding.
static byte[] decode(String pem)
          Decodes a PEM-encoded cryptographic object into the raw bytes of its ASN.1 encoding.
static Certificate decodeCert(String pemCert)
          Decodes the given certificate from PEM format.
static PublicKey decodeKey(String pemKey)
          Decodes the given public key from PEM format.
static PrivateKey decodeKey(String pemKey, char[] password)
          Decodes the given private key from PEM format.
static String encodeCert(Certificate key)
          Encodes the given certificate to PEM format.
static String encodeKey(PrivateKey key, char[] password, SecureRandom random)
          Encodes the given private key to PEM format.
static String encodeKey(PublicKey key)
          Encodes the given public key to PEM format.
static boolean isBase64Char(byte b)
          Determines whether the given byte represents an ASCII character in the character set for base64 encoding.
static boolean isPem(byte[] data)
          Determines whether the data in the given byte array is base64-encoded data of PEM encoding.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

KEY_ENCRYPTION_ALGORITHM

public static final String KEY_ENCRYPTION_ALGORITHM
Encryption algorithm used for password-protected private keys.

See Also:
Constant Field Values

HEADER_BEGIN

public static final String HEADER_BEGIN
PEM encoding header start string.

See Also:
Constant Field Values

FOOTER_END

public static final String FOOTER_END
PEM encoding footer start string.

See Also:
Constant Field Values

PROC_TYPE

public static final String PROC_TYPE
Procedure type tag for PEM-encoded private key in OpenSSL format.

See Also:
Constant Field Values

DEK_INFO

public static final String DEK_INFO
Decryption infor tag for PEM-encoded private key in OpenSSL format.

See Also:
Constant Field Values
Constructor Detail

PemHelper

protected PemHelper()
Hidden constructor of utility class.

Method Detail

decode

public static byte[] decode(byte[] pem)
                     throws IOException
Decodes a PEM-encoded cryptographic object into the raw bytes of its ASN.1 encoding. Header/footer data and metadata info, e.g. Proc-Type, are ignored.

Parameters:
pem - Bytes of PEM-encoded data to decode.
Returns:
ASN.1 encoded bytes.
Throws:
IOException - On decoding error.

decode

public static byte[] decode(String pem)
                     throws IOException
Decodes a PEM-encoded cryptographic object into the raw bytes of its ASN.1 encoding. Header/footer data and metadata info, e.g. Proc-Type, are ignored.

Parameters:
pem - PEM-encoded data to decode.
Returns:
ASN.1 encoded bytes.
Throws:
IOException - On decoding error.

encodeKey

public static String encodeKey(PublicKey key)
                        throws IOException
Encodes the given public key to PEM format.

Parameters:
key - Public key to encode.
Returns:
Key as PEM-encoded text.
Throws:
IOException - On encoding error.

decodeKey

public static PublicKey decodeKey(String pemKey)
                           throws IOException
Decodes the given public key from PEM format.

Parameters:
pemKey - PEM-encoded public key text to decode.
Returns:
Public key.
Throws:
IOException - On decoding error.

encodeKey

public static String encodeKey(PrivateKey key,
                               char[] password,
                               SecureRandom random)
                        throws IOException
Encodes the given private key to PEM format.

Parameters:
key - Private key to encode.
password - Password used to encrypt private key using 256-bit AES encryption; may be null to indicate no encryption.
random - Secure random provider used for encrypting private key.
Returns:
Key as PEM-encoded text.
Throws:
IOException - On encoding error.

decodeKey

public static PrivateKey decodeKey(String pemKey,
                                   char[] password)
                            throws IOException
Decodes the given private key from PEM format.

Parameters:
pemKey - PEM-encoded private key text to decode.
password - Optional password that is used to decrypt private key using DESEDE algorithm when specified.
Returns:
Private key.
Throws:
IOException - On decoding error.

encodeCert

public static String encodeCert(Certificate key)
                         throws IOException
Encodes the given certificate to PEM format.

Parameters:
key - Certificate to encode.
Returns:
Certificate as PEM-encoded text.
Throws:
IOException - On encoding error.

decodeCert

public static Certificate decodeCert(String pemCert)
                              throws IOException
Decodes the given certificate from PEM format.

Parameters:
pemCert - PEM-encoded certificate text to decode.
Returns:
Certificate.
Throws:
IOException - On decoding error.

isPem

public static boolean isPem(byte[] data)
Determines whether the data in the given byte array is base64-encoded data of PEM encoding. The determination is made using as little data from the given array as necessary to make a reasonable determination about encoding.

Parameters:
data - Data to test for PEM encoding
Returns:
True if data appears to be PEM encoded, false otherwise.

isBase64Char

public static boolean isBase64Char(byte b)
Determines whether the given byte represents an ASCII character in the character set for base64 encoding.

Parameters:
b - Byte to test.
Returns:
True if the byte represents an ASCII character in the set of valid characters for base64 encoding, false otherwise. The padding character '=' is not considered valid since it may only appear at the end of a base64 encoded value.


Copyright © 2003-2013 Virginia Tech. All Rights Reserved.