KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > jce > provider > JCERSAPublicKey


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.jce.provider;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.math.BigInteger JavaDoc;
23 import java.security.interfaces.RSAPublicKey JavaDoc;
24 import java.security.spec.RSAPublicKeySpec JavaDoc;
25
26 import org.apache.geronimo.util.asn1.ASN1Sequence;
27 import org.apache.geronimo.util.asn1.DERNull;
28 import org.apache.geronimo.util.asn1.DEROutputStream;
29 import org.apache.geronimo.util.asn1.pkcs.PKCSObjectIdentifiers;
30 import org.apache.geronimo.util.asn1.x509.AlgorithmIdentifier;
31 import org.apache.geronimo.util.asn1.x509.RSAPublicKeyStructure;
32 import org.apache.geronimo.util.asn1.x509.SubjectPublicKeyInfo;
33 import org.apache.geronimo.util.crypto.params.RSAKeyParameters;
34
35 public class JCERSAPublicKey
36     implements RSAPublicKey JavaDoc
37 {
38     private BigInteger JavaDoc modulus;
39     private BigInteger JavaDoc publicExponent;
40
41     JCERSAPublicKey(
42         RSAKeyParameters key)
43     {
44         this.modulus = key.getModulus();
45         this.publicExponent = key.getExponent();
46     }
47
48     JCERSAPublicKey(
49         RSAPublicKeySpec JavaDoc spec)
50     {
51         this.modulus = spec.getModulus();
52         this.publicExponent = spec.getPublicExponent();
53     }
54
55     JCERSAPublicKey(
56         RSAPublicKey JavaDoc key)
57     {
58         this.modulus = key.getModulus();
59         this.publicExponent = key.getPublicExponent();
60     }
61
62     JCERSAPublicKey(
63         SubjectPublicKeyInfo info)
64     {
65         try
66         {
67             RSAPublicKeyStructure pubKey = new RSAPublicKeyStructure((ASN1Sequence)info.getPublicKey());
68
69             this.modulus = pubKey.getModulus();
70             this.publicExponent = pubKey.getPublicExponent();
71         }
72         catch (IOException JavaDoc e)
73         {
74             throw new IllegalArgumentException JavaDoc("invalid info structure in RSA public key");
75         }
76     }
77
78     /**
79      * return the modulus.
80      *
81      * @return the modulus.
82      */

83     public BigInteger JavaDoc getModulus()
84     {
85         return modulus;
86     }
87
88     /**
89      * return the public exponent.
90      *
91      * @return the public exponent.
92      */

93     public BigInteger JavaDoc getPublicExponent()
94     {
95         return publicExponent;
96     }
97
98     public String JavaDoc getAlgorithm()
99     {
100         return "RSA";
101     }
102
103     public String JavaDoc getFormat()
104     {
105         return "X.509";
106     }
107
108     public byte[] getEncoded()
109     {
110         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
111         DEROutputStream dOut = new DEROutputStream(bOut);
112         SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(new AlgorithmIdentifier(PKCSObjectIdentifiers.rsaEncryption, new DERNull()), new RSAPublicKeyStructure(getModulus(), getPublicExponent()).getDERObject());
113
114         try
115         {
116             dOut.writeObject(info);
117             dOut.close();
118         }
119         catch (IOException JavaDoc e)
120         {
121             throw new RuntimeException JavaDoc("Error encoding RSA public key");
122         }
123
124         return bOut.toByteArray();
125
126     }
127
128     public boolean equals(Object JavaDoc o)
129     {
130         if ( !(o instanceof RSAPublicKey JavaDoc) )
131         {
132             return false;
133         }
134
135         if ( o == this )
136         {
137             return true;
138         }
139
140         RSAPublicKey JavaDoc key = (RSAPublicKey JavaDoc)o;
141
142         return getModulus().equals(key.getModulus())
143             && getPublicExponent().equals(key.getPublicExponent());
144     }
145
146     public String JavaDoc toString()
147     {
148         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
149         String JavaDoc nl = System.getProperty("line.separator");
150
151         buf.append("RSA Public Key" + nl);
152         buf.append(" modulus: " + this.getModulus().toString(16) + nl);
153         buf.append(" public exponent: " + this.getPublicExponent().toString(16) + nl);
154
155         return buf.toString();
156     }
157 }
158
Popular Tags