KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.security.KeyPair JavaDoc;
24 import java.security.KeyPairGenerator JavaDoc;
25 import java.security.NoSuchAlgorithmException JavaDoc;
26 import java.security.interfaces.RSAPrivateKey JavaDoc;
27 import java.security.interfaces.RSAPublicKey JavaDoc;
28
29 import com.sslexplorer.security.pki.InvalidKeyException;
30 import com.sslexplorer.security.pki.SshKeyPair;
31 import com.sslexplorer.security.pki.SshPrivateKey;
32 import com.sslexplorer.security.pki.SshPublicKey;
33 import com.sslexplorer.security.pki.Utils;
34
35
36 /**
37  *
38  *
39  * @author $author$
40  */

41 public class SshRsaKeyPair extends SshKeyPair {
42     private RSAPrivateKey JavaDoc prvKey;
43     private RSAPublicKey JavaDoc pubKey;
44
45     /**
46      * Creates a new SshRsaKeyPair object.
47      */

48     public SshRsaKeyPair() {
49     }
50
51     /**
52      *
53      *
54      * @param encoded
55      *
56      * @return
57      *
58      * @throws InvalidKeyException
59      */

60     public SshPrivateKey decodePrivateKey(byte[] encoded)
61         throws InvalidKeyException {
62         return new SshRsaPrivateKey(encoded);
63     }
64
65     /**
66      *
67      *
68      * @param encoded
69      *
70      * @return
71      *
72      * @throws InvalidKeyException
73      */

74     public SshPublicKey decodePublicKey(byte[] encoded)
75         throws InvalidKeyException {
76         return new SshRsaPublicKey(encoded);
77     }
78
79     /**
80      *
81      *
82      * @param bits
83      */

84     public void generate(int bits) {
85         try {
86             // Initialize the generator
87
KeyPairGenerator JavaDoc keyGen = KeyPairGenerator.getInstance("RSA");
88             keyGen.initialize(bits, Utils.getRND());
89
90             KeyPair JavaDoc pair = keyGen.generateKeyPair();
91
92             // Get the keys and set
93
setPrivateKey(new SshRsaPrivateKey(
94                     (RSAPrivateKey JavaDoc) pair.getPrivate(),
95                     (RSAPublicKey JavaDoc) pair.getPublic()));
96         } catch (NoSuchAlgorithmException JavaDoc nsae) {
97             prvKey = null;
98             pubKey = null;
99         }
100     }
101 }
102
Popular Tags