1    package org.bouncycastle.asn1.x509;
2    
3    import java.util.Enumeration;
4    import java.math.BigInteger;
5    
6    import org.bouncycastle.asn1.*;
7    
8    public class RSAPublicKeyStructure
9        implements DEREncodable
10   {
11       private BigInteger  modulus;
12       private BigInteger  publicExponent;
13   
14       public static RSAPublicKeyStructure getInstance(
15           ASN1TaggedObject obj,
16           boolean          explicit)
17       {
18           return getInstance(ASN1Sequence.getInstance(obj, explicit));
19       }
20   
21       public static RSAPublicKeyStructure getInstance(
22           Object obj)
23       {
24           if(obj == null || obj instanceof RSAPublicKeyStructure) 
25           {
26               return (RSAPublicKeyStructure)obj;
27           }
28           
29           if(obj instanceof ASN1Sequence) 
30           {
31               return new RSAPublicKeyStructure((ASN1Sequence)obj);
32           }
33           
34           throw new IllegalArgumentException("Invalid RSAPublicKeyStructure: " + obj.getClass().getName());
35       }
36       
37       public RSAPublicKeyStructure(
38           BigInteger  modulus,
39           BigInteger  publicExponent)
40       {
41           this.modulus = modulus;
42           this.publicExponent = publicExponent;
43       }
44   
45       public RSAPublicKeyStructure(
46           ASN1Sequence  seq)
47       {
48           Enumeration e = seq.getObjects();
49   
50           modulus = ((DERInteger)e.nextElement()).getValue();
51           publicExponent = ((DERInteger)e.nextElement()).getValue();
52       }
53   
54       public BigInteger getModulus()
55       {
56           return modulus;
57       }
58   
59       public BigInteger getPublicExponent()
60       {
61           return publicExponent;
62       }
63   
64       /**
65        * This outputs the key in PKCS1v2 format.
66        * <pre>
67        *      RSAPublicKey ::= SEQUENCE {
68        *                          modulus INTEGER, -- n
69        *                          publicExponent INTEGER, -- e
70        *                      }
71        * </pre>
72        * <p>
73        */
74       public DERObject getDERObject()
75       {
76           DERConstructedSequence  seq = new DERConstructedSequence();
77   
78           seq.addObject(new DERInteger(getModulus()));
79           seq.addObject(new DERInteger(getPublicExponent()));
80   
81           return seq;
82       }
83   }
84