FileDocCategorySizeDatePackage
KeyRep.javaAPI DocJava SE 5 API4874Fri Aug 26 14:57:14 BST 2005java.security

KeyRep

public class KeyRep extends Object implements Serializable
Standardized representation for serialized Key objects.

Note that a serialized Key may contain sensitive information which should not be exposed in untrusted environments. See the Security Appendix of the Serialization Specification for more information.

see
Key
see
KeyFactory
see
javax.crypto.spec.SecretKeySpec
see
java.security.spec.X509EncodedKeySpec
see
java.security.spec.PKCS8EncodedKeySpec
version
1.7, 04/04/20
since
1.5

Fields Summary
private static final long
serialVersionUID
private static final String
PKCS8
Type for private keys.
private static final String
X509
private static final String
RAW
private Type
type
Either one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE
private String
algorithm
The Key algorithm
private String
format
The Key encoding format
private byte[]
encoded
The encoded Key bytes
Constructors Summary
public KeyRep(Type type, String algorithm, String format, byte[] encoded)
Construct the alternate Key class.

param
type either one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE
param
algorithm the algorithm returned from Key.getAlgorithm()
param
format the encoding format returned from Key.getFormat()
param
encoded the encoded bytes returned from Key.getEncoded()
exception
NullPointerException if type is null, if algorithm is null, if format is null, or if encoded is null

 
                         		       		       		  		   		   		   		         
        
		    

	if (type == null || algorithm == null ||
	    format == null || encoded == null) {
	    throw new NullPointerException("invalid null input(s)");
	}

	this.type = type;
	this.algorithm = algorithm;
	this.format = format.toUpperCase();
	this.encoded = (byte[])encoded.clone();
    
Methods Summary
protected java.lang.ObjectreadResolve()
Resolve the Key object.

This method supports three Type/format combinations:

  • Type.SECRET/"RAW" - returns a SecretKeySpec object constructed using encoded key bytes and algorithm
  • Type.PUBLIC/"X.509" - gets a KeyFactory instance for the key algorithm, constructs an X509EncodedKeySpec with the encoded key bytes, and generates a public key from the spec
  • Type.PRIVATE/"PKCS#8" - gets a KeyFactory instance for the key algorithm, constructs a PKCS8EncodedKeySpec with the encoded key bytes, and generates a private key from the spec

return
the resolved Key object
exception
NotSerializableException if the Type/format combination is unrecognized, if the algorithm, key format, or encoded key bytes are unrecognized/invalid, of if the resolution of the key fails for any reason

	try {
	    if (type == Type.SECRET && RAW.equals(format)) {
		return new SecretKeySpec(encoded, algorithm);
	    } else if (type == Type.PUBLIC && X509.equals(format)) {
		KeyFactory f = KeyFactory.getInstance(algorithm);
		return f.generatePublic(new X509EncodedKeySpec(encoded));
	    } else if (type == Type.PRIVATE && PKCS8.equals(format)) {
		KeyFactory f = KeyFactory.getInstance(algorithm);
		return f.generatePrivate(new PKCS8EncodedKeySpec(encoded));
	    } else {
		throw new NotSerializableException
			("unrecognized type/format combination: " +
			type + "/" + format);
	    }
	} catch (NotSerializableException nse) {
	    throw nse;
	} catch (Exception e) {
	    NotSerializableException nse = new NotSerializableException
					("java.security.Key: " +
					"[" + type + "] " +
					"[" + algorithm + "] " +
					"[" + format + "]");
	    nse.initCause(e);
	    throw nse;
	}