KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > maverick > crypto > asn1 > x509 > RSAPublicKeyStructure


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.maverick.crypto.asn1.x509;
21
22 import java.math.BigInteger JavaDoc;
23 import java.util.Enumeration JavaDoc;
24
25 import com.maverick.crypto.asn1.ASN1EncodableVector;
26 import com.maverick.crypto.asn1.ASN1Sequence;
27 import com.maverick.crypto.asn1.ASN1TaggedObject;
28 import com.maverick.crypto.asn1.DEREncodable;
29 import com.maverick.crypto.asn1.DERInteger;
30 import com.maverick.crypto.asn1.DERObject;
31 import com.maverick.crypto.asn1.DERSequence;
32
33 public class RSAPublicKeyStructure
34     implements DEREncodable
35 {
36     private BigInteger JavaDoc modulus;
37     private BigInteger JavaDoc publicExponent;
38
39     public static RSAPublicKeyStructure getInstance(
40         ASN1TaggedObject obj,
41         boolean explicit)
42     {
43         return getInstance(ASN1Sequence.getInstance(obj, explicit));
44     }
45
46     public static RSAPublicKeyStructure getInstance(
47         Object JavaDoc obj)
48     {
49         if(obj == null || obj instanceof RSAPublicKeyStructure)
50         {
51             return (RSAPublicKeyStructure)obj;
52         }
53
54         if(obj instanceof ASN1Sequence)
55         {
56             return new RSAPublicKeyStructure((ASN1Sequence)obj);
57         }
58
59         throw new IllegalArgumentException JavaDoc("Invalid RSAPublicKeyStructure: " + obj.getClass().getName());
60     }
61
62     public RSAPublicKeyStructure(
63         BigInteger JavaDoc modulus,
64         BigInteger JavaDoc publicExponent)
65     {
66         this.modulus = modulus;
67         this.publicExponent = publicExponent;
68     }
69
70     public RSAPublicKeyStructure(
71         ASN1Sequence seq)
72     {
73         Enumeration JavaDoc e = seq.getObjects();
74
75         modulus = ((DERInteger)e.nextElement()).getPositiveValue();
76         publicExponent = ((DERInteger)e.nextElement()).getPositiveValue();
77     }
78
79     public BigInteger JavaDoc getModulus()
80     {
81         return modulus;
82     }
83
84     public BigInteger JavaDoc getPublicExponent()
85     {
86         return publicExponent;
87     }
88
89     /**
90      * This outputs the key in PKCS1v2 format.
91      * <pre>
92      * RSAPublicKey ::= SEQUENCE {
93      * modulus INTEGER, -- n
94      * publicExponent INTEGER, -- e
95      * }
96      * </pre>
97      * <p>
98      */

99     public DERObject getDERObject()
100     {
101         ASN1EncodableVector v = new ASN1EncodableVector();
102
103         v.add(new DERInteger(getModulus()));
104         v.add(new DERInteger(getPublicExponent()));
105
106         return new DERSequence(v);
107     }
108 }
109
Popular Tags