KeyReppublic class KeyRep extends Object implements Serializable{@code KeyRep} is a standardized representation for serialized {@link Key}
objects. |
Fields Summary |
---|
private static final long | serialVersionUID | private final Type | type | private final String | algorithm | private final String | format | private byte[] | encoded |
Constructors Summary |
---|
public KeyRep(Type type, String algorithm, String format, byte[] encoded)Constructs a new instance of {@code KeyRep} with the specified arguments.
The arguments should be obtained from the {@code Key} object that has to
be serialized.
this.type = type;
this.algorithm = algorithm;
this.format = format;
this.encoded = encoded;
if(this.type == null) {
throw new NullPointerException(Messages.getString("security.07")); //$NON-NLS-1$
}
if(this.algorithm == null) {
throw new NullPointerException(Messages.getString("security.08")); //$NON-NLS-1$
}
if(this.format == null) {
throw new NullPointerException(Messages.getString("security.09")); //$NON-NLS-1$
}
if(this.encoded == null) {
throw new NullPointerException(Messages.getString("security.0A")); //$NON-NLS-1$
}
|
Methods Summary |
---|
private void | readObject(java.io.ObjectInputStream is)
is.defaultReadObject();
byte[] new_encoded = new byte[encoded.length];
System.arraycopy(encoded, 0, new_encoded, 0, new_encoded.length);
encoded = new_encoded;
| protected java.lang.Object | readResolve()Resolves and returns the {@code Key} object. Three {@link Type}|format
combinations are supported:
- {@code Type.PRIVATE} | "PKCS#8" : returns a {@link PrivateKey}
instance, generated from a key factory (suitable for the algorithm) that
is initialized with a {@link PKCS8EncodedKeySpec} using the encoded key
bytes.
- {@code Type.SECRET} | "RAW" : returns a {@link SecretKeySpec}
instance, created with the encoded key bytes and the algorithm.
- {@code Type.PUBLIC} | "X.509": returns a {@link PublicKey} instance,
generated from a key factory (suitable for the algorithm) that is
initialized with a {@link X509EncodedKeySpec} using the encoded key
bytes.
switch (type) {
case SECRET:
if ("RAW".equals(format)) { //$NON-NLS-1$
try {
return new SecretKeySpec(encoded, algorithm);
} catch (IllegalArgumentException e) {
throw new NotSerializableException(
Messages.getString("security.0B", e)); //$NON-NLS-1$
}
}
throw new NotSerializableException(
Messages.getString("security.0C", type, format)); //$NON-NLS-1$
case PUBLIC:
if ("X.509".equals(format)) { //$NON-NLS-1$
try {
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePublic(new X509EncodedKeySpec(encoded));
} catch (NoSuchAlgorithmException e) {
throw new NotSerializableException(
Messages.getString("security.0D", e)); //$NON-NLS-1$
}
catch (InvalidKeySpecException e) {
throw new NotSerializableException(
Messages.getString("security.0D", e)); //$NON-NLS-1$
}
}
throw new NotSerializableException(
Messages.getString("security.0C", type, format)); //$NON-NLS-1$
case PRIVATE:
if ("PKCS#8".equals(format)) { //$NON-NLS-1$
try {
KeyFactory kf = KeyFactory.getInstance(algorithm);
return kf.generatePrivate(new PKCS8EncodedKeySpec(encoded));
} catch (NoSuchAlgorithmException e) {
throw new NotSerializableException(
Messages.getString("security.0D", e)); //$NON-NLS-1$
}
catch (InvalidKeySpecException e) {
throw new NotSerializableException(
Messages.getString("security.0D", e)); //$NON-NLS-1$
}
}
throw new NotSerializableException(
Messages.getString("security.0C", type, format)); //$NON-NLS-1$
}
throw new NotSerializableException(Messages.getString("security.0E", type)); //$NON-NLS-1$
|
|