KeyReppublic class KeyRep extends Object implements SerializableStandardized 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. |
Fields Summary |
---|
private static final long | serialVersionUID | private static final String | PKCS8Type for private keys. | private static final String | X509 | private static final String | RAW | private Type | typeEither one of Type.SECRET, Type.PUBLIC, or Type.PRIVATE | private String | algorithmThe Key algorithm | private String | formatThe Key encoding format | private byte[] | encodedThe encoded Key bytes |
Constructors Summary |
---|
public KeyRep(Type type, String algorithm, String format, byte[] encoded)Construct the alternate Key class.
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.Object | readResolve()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
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;
}
|
|