FileDocCategorySizeDatePackage
RSAPublicKey.javaAPI DocphoneME MR2 API (J2ME)5192Wed May 02 18:00:38 BST 2007com.sun.satsa.crypto

RSAPublicKey

public class RSAPublicKey extends Object implements PublicKey
This class represents an RSA public key.

Fields Summary
private static byte[]
RSA_OID
OID for RSA crypto algorithm, 1.2.840.113549.1.1.1.
private com.sun.midp.crypto.RSAPublicKey
key
Key object.
private int
keyLen
Key length in bytes.
private X509EncodedKeySpec
keySpec
Specification of the key material.
Constructors Summary
public RSAPublicKey(KeySpec keySpec)
Constructs an RSAPublicKey object.

param
keySpec specification of the key material
throws
InvalidKeySpecException if key specification is invalid


                           
         

        if (! (keySpec instanceof X509EncodedKeySpec)) {
            throw new InvalidKeySpecException();
        }

        this.keySpec = (X509EncodedKeySpec) keySpec;

        byte[] data = getEncoded();

        /*
         * SubjectPublicKeyInfo { ALGORITHM : IOSet} ::= SEQUENCE {
         *       algorithm        AlgorithmIdentifier {{IOSet}},
         *       subjectPublicKey BIT STRING
         *  }
         *
         *  AlgorithmIdentifier  ::=  SEQUENCE  {
         *       algorithm               OBJECT IDENTIFIER,
         *       parameters              ANY DEFINED BY algorithm OPTIONAL
         *  }
         *
         */

        try {
            TLV t = new TLV(data, 0);

            t = t.child;    // AlgorithmIdentifier
            if (! Utils.byteMatch(data, t.child.valueOffset, t.child.length,
                                 RSA_OID, 0, RSA_OID.length)) {
                throw new InvalidKeySpecException(
                        "Invalid algorithm identifier");
            }

            t = t.next;     // subjectPublicKey

            /*
             *  RSAPublicKey ::= SEQUENCE {
             *      modulus            INTEGER, -- n
             *      publicExponent     INTEGER  -- e --
             *  }
             */

            // the first byte of value in BIT STRING is the number of
            // unused bits
            t = new TLV(data, t.valueOffset + 1);

            t = t.child;    // modulus

            int offset = t.valueOffset;
            int len = t.length;
            while (data[offset] == 0) {
                offset++;
                len--;
            }

            keyLen = ((len + 7) / 8) * 8;

            t = t.next;

            key = new com.sun.midp.crypto.RSAPublicKey(data, offset, len,
                      data, t.valueOffset, t.length);
        } catch (InvalidKeySpecException ikse) {
            throw ikse;
        } catch (NullPointerException npe) {
            throw new InvalidKeySpecException();
        } catch (TLVException tlve) {
            throw new InvalidKeySpecException();
        }
    
Methods Summary
public java.lang.StringgetAlgorithm()
Returns the standard algorithm name for this key.

return
the name of the algorithm associated with this key.

        return "RSA";
    
public byte[]getEncoded()
Returns the key in its primary encoding format, or null if this key does not support encoding.

return
the encoded key.

        return keySpec.getEncoded();
    
public java.lang.StringgetFormat()
Returns the name of the primary encoding format of this key.

return
the primary encoding format of the key.

        return keySpec.getFormat();
    
public com.sun.midp.crypto.RSAPublicKeygetKey()
Returns key object.

return
key object

        return key;
    
public intgetKeySize()
Returns the size of key in bytes.

return
size of key in bytes

        return keyLen;