KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > security > pki > SshKeyGenerator


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;
21
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25
26 import com.sslexplorer.boot.Util;
27
28 public class SshKeyGenerator {
29
30
31
32     /**
33      * Creates a new SshKeyGenerator object.
34      */

35     public SshKeyGenerator() {
36     }
37
38     /**
39      *
40      *
41      * @param type
42      * @param bits
43      * @param filename
44      * @param username
45      * @param passphrase
46      *
47      * @throws IOException
48      */

49     public static void generateKeyPair(String JavaDoc type, int bits,
50         String JavaDoc username, String JavaDoc passphrase, OutputStream JavaDoc prvOut, OutputStream JavaDoc pubOut) throws IOException JavaDoc, InvalidKeyException {
51         
52         String JavaDoc keyType = type;
53
54         if (keyType.equalsIgnoreCase("DSA")) {
55             keyType = "ssh-dss";
56         }
57
58         if (keyType.equalsIgnoreCase("RSA")) {
59             keyType = "ssh-rsa";
60         }
61
62         final SshKeyPair pair = SshKeyPairFactory.newInstance(keyType);
63         System.out.println("Generating " + String.valueOf(bits) + " bit " +
64             keyType + " key pair");
65
66         pair.generate(bits);
67         
68         // Now save the files
69
SshPublicKeyFile pub = SshPublicKeyFile.create(pair.getPublicKey(),
70                 new SECSHPublicKeyFormat(username,
71                     String.valueOf(bits) + "-bit " + type));
72         
73         pubOut.write(pub.getBytes());
74         Util.closeStream(pubOut);
75
76
77         SshPrivateKeyFile prv = SshPrivateKeyFile.create(pair.getPrivateKey(),
78                 passphrase,
79                 new SshtoolsPrivateKeyFormat(username,
80                     String.valueOf(bits) + "-bit " + type));
81         prvOut.write(prv.getBytes());
82         Util.closeStream(prvOut);
83     }
84
85
86     /**
87      *
88      *
89      * @param f
90      * @param oldPassphrase
91      * @param newPassphrase
92      *
93      * @throws IOException
94      * @throws InvalidKeyException
95      */

96     public static void changePassphrase(InputStream JavaDoc prvIn, OutputStream JavaDoc prvOut, String JavaDoc oldPassphrase,
97         String JavaDoc newPassphrase) throws IOException JavaDoc, InvalidKeyException {
98         // Open up the file with its current format
99
SshPrivateKeyFile file = SshPrivateKeyFile.parse(prvIn);
100         file.changePassphrase(oldPassphrase, newPassphrase);
101         
102         Util.closeStream(prvIn);
103         
104         try {
105             prvOut.write(file.getBytes());
106         } finally {
107             Util.closeStream(prvOut);
108         }
109     }
110
111
112
113
114 }
115
Popular Tags