KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jdesktop > swing > auth > KeyChain


1 package org.jdesktop.swing.auth;
2
3 /*
4  * $Id: KeyChain.java,v 1.2 2005/01/28 01:15:33 bino_george Exp $
5  *
6  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
7  * Santa Clara, California 95054, U.S.A. All rights reserved.
8  */

9
10 import java.io.EOFException JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.IOException JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.OutputStream JavaDoc;
17 import java.security.KeyStore JavaDoc;
18 import java.security.KeyStoreException JavaDoc;
19 import java.security.NoSuchAlgorithmException JavaDoc;
20 import java.security.UnrecoverableEntryException JavaDoc;
21 import java.security.cert.CertificateException JavaDoc;
22
23 import javax.crypto.spec.SecretKeySpec;
24
25 /* <p>
26  * <b>KeyChain</b> is a class that implements the "KeyChain" concept.
27  * Fundamentally, it allows you to store multiple keys/credentials
28  * in a central password store. Access to this central store is
29  * controlled through a master password. This mechanism is used in
30  * many popular client applications where you need to store credentials
31  * for multiple servers/accounts. The actual store for the KeyStore
32  * can be any OutputStream and it can work in the webstart sandbox
33  * using Muffins.
34  * </p>
35  * <p>
36  * To contstruct a <b>KeyChain</b>, you need to pass in an InputStream to the
37  * store and it will initialize the KeyStore from the InputStream.
38  * You can add and remove entries any time once you have an instance of
39  * KeyChain. To persist the KeyChain and reflect any changes, you need to
40  * call <b>store</b> method with an OutputStream.
41  * </p>
42  *
43  * @author Bino George
44  */

45 public class KeyChain {
46     private KeyStore JavaDoc store;
47
48     private char[] masterPassword;
49
50     /**
51      * Creates an instance of KeyChain and initializes the store
52      * from the InputStream.
53      *
54      * @param masterPassword
55      * @param inputStream
56      * @throws IOException
57      */

58     public KeyChain(char[] masterPassword, InputStream JavaDoc inputStream)
59             throws IOException JavaDoc {
60         this.masterPassword = masterPassword;
61
62         try {
63             store = KeyStore.getInstance("JCEKS");
64             store.load(inputStream, masterPassword);
65
66         } catch (KeyStoreException JavaDoc e) {
67             e.printStackTrace();
68         } catch (CertificateException JavaDoc e) {
69             e.printStackTrace();
70         } catch (NoSuchAlgorithmException JavaDoc e) {
71             e.printStackTrace();
72         } catch (EOFException JavaDoc e) {
73             e.printStackTrace();
74         }
75
76     }
77
78     /**
79      * Fetches the password for a given account/user and server.
80      * @param user
81      * @param server
82      * @return
83      */

84     public String JavaDoc getPassword(String JavaDoc user, String JavaDoc server) {
85
86         try {
87
88             KeyStore.SecretKeyEntry JavaDoc entry2 = (KeyStore.SecretKeyEntry JavaDoc) store
89                     .getEntry(user + "@" + server,
90                             new KeyStore.PasswordProtection JavaDoc(masterPassword));
91             return new String JavaDoc(entry2.getSecretKey().getEncoded());
92         } catch (KeyStoreException JavaDoc e) {
93             e.printStackTrace();
94         } catch (UnrecoverableEntryException JavaDoc ce) {
95             ce.printStackTrace();
96         } catch (NoSuchAlgorithmException JavaDoc ne) {
97             ne.printStackTrace();
98         }
99
100         return null;
101     }
102
103     /**
104      * Adds a password to the KeyChain for a given account/user and server.
105      *
106      * @param user
107      * @param server
108      * @param password
109      * @throws IOException
110      */

111     public void addPassword(String JavaDoc user, String JavaDoc server, char[] password)
112             throws IOException JavaDoc {
113         String JavaDoc pass = new String JavaDoc(password);
114         SecretKeySpec passwordKey = new SecretKeySpec(pass.getBytes(), "JCEKS");
115         KeyStore.SecretKeyEntry JavaDoc entry = new KeyStore.SecretKeyEntry JavaDoc(passwordKey);
116         try {
117             store.setEntry(user + "@" + server, entry,
118                     new KeyStore.PasswordProtection JavaDoc(masterPassword));
119         } catch (KeyStoreException JavaDoc e) {
120             e.printStackTrace();
121         }
122     }
123
124     /**
125      * Removes a password for a given account/user and server.
126      *
127      * @param user
128      * @param server
129      */

130     public void removePassword(String JavaDoc user, String JavaDoc server) {
131         try {
132             store.deleteEntry(user + "@" + server);
133         } catch (KeyStoreException JavaDoc e) {
134             e.printStackTrace();
135         }
136     }
137
138     /**
139      * Persists the KeyChain to an OutputStream
140      *
141      * @param ostream
142      * @throws IOException
143      */

144
145     public void store(OutputStream JavaDoc ostream) throws IOException JavaDoc {
146         try {
147             store.store(ostream, masterPassword);
148         } catch (KeyStoreException JavaDoc e) {
149             e.printStackTrace();
150         } catch (CertificateException JavaDoc ce) {
151             ce.printStackTrace();
152         } catch (NoSuchAlgorithmException JavaDoc ne) {
153             ne.printStackTrace();
154         }
155     }
156
157
158     public static void main(String JavaDoc[] args) {
159         try {
160             File JavaDoc file = new File JavaDoc("c:\\test.txt");
161             FileInputStream JavaDoc fis;
162             if (!file.exists()) {
163                 file.createNewFile();
164                 fis = null;
165             } else {
166                 fis = new FileInputStream JavaDoc(file);
167             }
168             KeyChain kc = new KeyChain("test".toCharArray(), fis);
169             kc.addPassword("bino", "sun-ds.sfbay", "test123".toCharArray());
170             System.out.println("pass = "
171                     + kc.getPassword("bino", "sun-ds.sfbay"));
172
173             System.out.println("More testing :");
174             for (int i = 0; i < 100; i++) {
175                 kc.addPassword("" + i, "sun-ds.sfbay", ("" + i).toCharArray());
176             }
177             for (int i = 0; i < 100; i++) {
178                 System.out.println("key =" + i + " pass ="
179                         + kc.getPassword("" + i, "sun-ds.sfbay"));
180             }
181             kc.store(new FileOutputStream JavaDoc(file));
182         } catch (Exception JavaDoc e) {
183             e.printStackTrace();
184         }
185     }
186     
187 }
188
Popular Tags