KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > pki > rsa > SshRsaPrivateKey


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.sslexplorer.security.pki.rsa;
21
22
23
24 import java.io.IOException JavaDoc;
25 import java.math.BigInteger JavaDoc;
26 import java.security.KeyFactory JavaDoc;
27 import java.security.PrivateKey JavaDoc;
28 import java.security.Signature JavaDoc;
29 import java.security.interfaces.RSAPrivateKey JavaDoc;
30 import java.security.interfaces.RSAPublicKey JavaDoc;
31 import java.security.spec.RSAPrivateKeySpec JavaDoc;
32 import java.security.spec.RSAPublicKeySpec JavaDoc;
33
34 import com.maverick.util.ByteArrayReader;
35 import com.maverick.util.ByteArrayWriter;
36 import com.sslexplorer.security.pki.InvalidKeyException;
37 import com.sslexplorer.security.pki.SshPrivateKey;
38 import com.sslexplorer.security.pki.SshPublicKey;
39
40
41 /**
42  *
43  *
44  * @author $author$
45  */

46 public class SshRsaPrivateKey extends SshPrivateKey {
47     RSAPrivateKey JavaDoc prvKey;
48     RSAPublicKey JavaDoc pubKey;
49
50     /**
51      * Creates a new SshRsaPrivateKey object.
52      *
53      * @param prv
54      * @param pub
55      */

56     public SshRsaPrivateKey(RSAPrivateKey JavaDoc prv, RSAPublicKey JavaDoc pub) {
57         prvKey = prv;
58         pubKey = pub;
59     }
60     
61     
62     public PrivateKey JavaDoc getPrivateKey() {
63         return prvKey;
64     }
65
66     /**
67      * Creates a new SshRsaPrivateKey object.
68      *
69      * @param encoded
70      *
71      * @throws InvalidKeyException
72      */

73     public SshRsaPrivateKey(byte[] encoded) throws InvalidKeyException {
74         try {
75             // Extract the key information
76
ByteArrayReader bar = new ByteArrayReader(encoded);
77
78             // Read the public key
79
String JavaDoc header = bar.readString();
80
81             if (!header.equals(getAlgorithmName())) {
82                 throw new InvalidKeyException();
83             }
84
85             BigInteger JavaDoc e = bar.readBigInteger();
86             BigInteger JavaDoc n = bar.readBigInteger();
87
88             // Read the private key
89
BigInteger JavaDoc p = bar.readBigInteger();
90             RSAPrivateKeySpec JavaDoc prvSpec = new RSAPrivateKeySpec JavaDoc(n, p);
91             RSAPublicKeySpec JavaDoc pubSpec = new RSAPublicKeySpec JavaDoc(n, e);
92             KeyFactory JavaDoc kf = KeyFactory.getInstance("RSA");
93             prvKey = (RSAPrivateKey JavaDoc) kf.generatePrivate(prvSpec);
94             pubKey = (RSAPublicKey JavaDoc) kf.generatePublic(pubSpec);
95         } catch (Exception JavaDoc e) {
96             throw new InvalidKeyException();
97         }
98     }
99
100     /**
101      *
102      *
103      * @param obj
104      *
105      * @return
106      */

107     public boolean equals(Object JavaDoc obj) {
108         if (obj instanceof SshRsaPrivateKey) {
109             return prvKey.equals(((SshRsaPrivateKey) obj).prvKey);
110         }
111
112         return false;
113     }
114
115     /**
116      *
117      *
118      * @return
119      */

120     public int hashCode() {
121         return prvKey.hashCode();
122     }
123
124     /**
125      *
126      *
127      * @return
128      */

129     public String JavaDoc getAlgorithmName() {
130         return "ssh-rsa";
131     }
132
133     /**
134      *
135      *
136      * @return
137      */

138     public int getBitLength() {
139         return prvKey.getModulus().bitLength();
140     }
141
142     /**
143      *
144      *
145      * @return
146      */

147     public byte[] getEncoded() {
148         try {
149             ByteArrayWriter baw = new ByteArrayWriter();
150
151             // The private key consists of the public key blob
152
baw.write(getPublicKey().getEncoded());
153
154             // And the private data
155
baw.writeBigInteger(prvKey.getPrivateExponent());
156
157             return baw.toByteArray();
158         } catch (IOException JavaDoc ioe) {
159             return null;
160         }
161     }
162
163     /**
164      *
165      *
166      * @return
167      */

168     public SshPublicKey getPublicKey() {
169         return new SshRsaPublicKey(pubKey);
170     }
171
172     /**
173      *
174      *
175      * @param data
176      *
177      * @return
178      */

179     public byte[] generateSignature(byte[] data) {
180         try {
181             Signature JavaDoc sig = Signature.getInstance("SHA1withRSA");
182             sig.initSign(prvKey);
183             sig.update(data);
184
185             ByteArrayWriter baw = new ByteArrayWriter();
186             baw.writeString(getAlgorithmName());
187             baw.writeBinaryString(sig.sign());
188
189             return baw.toByteArray();
190         } catch (Exception JavaDoc e) {
191             return null;
192         }
193     }
194 }
195
Popular Tags