FileDocCategorySizeDatePackage
KeyRep.javaAPI DocAndroid 1.5 API7405Wed May 06 22:41:06 BST 2009java.security

KeyRep

public class KeyRep extends Object implements Serializable
{@code KeyRep} is a standardized representation for serialized {@link Key} objects.
since
Android 1.0

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.

param
type the type of the key.
param
algorithm the algorithm (obtained by {@link Key#getAlgorithm()}).
param
format the format of the key (obtained by {@link Key#getFormat()}).
param
encoded the encoded {@code byte[]} (obtained by {@link Key#getEncoded()}).
throws
NullPointerException if {@code type, algorithm, format or encoded} is {@code null} .
since
Android 1.0


                                                                                                                                                                       
      
                  
        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 voidreadObject(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.ObjectreadResolve()
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.

return
the resolved {@code Key} object.
throws
ObjectStreamException if the {@code Type}|format combination is not recognized, or the resolution of any key parameter fails.
since
Android 1.0

        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$