KeyChain.java
上传用户:zhengdagz
上传日期:2014-03-06
资源大小:1956k
文件大小:6k
源码类别:

xml/soap/webservice

开发平台:

Java

  1. /*
  2.  * $Id: KeyChain.java,v 1.3 2005/10/10 18:02:48 rbair Exp $
  3.  *
  4.  * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
  5.  * Santa Clara, California 95054, U.S.A. All rights reserved.
  6.  *
  7.  * This library is free software; you can redistribute it and/or
  8.  * modify it under the terms of the GNU Lesser General Public
  9.  * License as published by the Free Software Foundation; either
  10.  * version 2.1 of the License, or (at your option) any later version.
  11.  * 
  12.  * This library is distributed in the hope that it will be useful,
  13.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  15.  * Lesser General Public License for more details.
  16.  * 
  17.  * You should have received a copy of the GNU Lesser General Public
  18.  * License along with this library; if not, write to the Free Software
  19.  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  20.  */
  21. package org.jdesktop.swingx.auth;
  22. import java.io.EOFException;
  23. import java.io.File;
  24. import java.io.FileInputStream;
  25. import java.io.FileOutputStream;
  26. import java.io.IOException;
  27. import java.io.InputStream;
  28. import java.io.OutputStream;
  29. import java.security.KeyStore;
  30. import java.security.KeyStoreException;
  31. import java.security.NoSuchAlgorithmException;
  32. import java.security.UnrecoverableEntryException;
  33. import java.security.cert.CertificateException;
  34. import javax.crypto.spec.SecretKeySpec;
  35. /**
  36.  * <b>KeyChain</b> is a class that implements the "KeyChain" concept.
  37.  * Fundamentally, it allows you to store multiple keys/credentials 
  38.  * in a central password store. Access to this central store is
  39.  * controlled through a master password. This mechanism is used in
  40.  * many popular client applications where you need to store credentials
  41.  * for multiple servers/accounts. The actual store for the KeyStore
  42.  * can be any OutputStream and it can work in the webstart sandbox
  43.  * using Muffins.
  44.  * </p>
  45.  * <p>
  46.  * To contstruct a <b>KeyChain</b>, you need to pass in an InputStream to the
  47.  * store and it will initialize the KeyStore from the InputStream.
  48.  * You can add and remove entries any time once you have an instance of
  49.  * KeyChain. To persist the KeyChain and reflect any changes, you need to
  50.  * call <b>store</b> method with an OutputStream.
  51.  * </p>
  52.  * 
  53.  * @author Bino George
  54.  */
  55. public class KeyChain {
  56. private KeyStore store;
  57. private char[] masterPassword;
  58. /**
  59.  * Creates an instance of KeyChain and initializes the store
  60.  * from the InputStream.
  61.  * 
  62.  * @param masterPassword
  63.  * @param inputStream
  64.  * @throws IOException
  65.  */
  66. public KeyChain(char[] masterPassword, InputStream inputStream)
  67. throws IOException {
  68. this.masterPassword = masterPassword;
  69. try {
  70. store = KeyStore.getInstance("JCEKS");
  71. store.load(inputStream, masterPassword);
  72. } catch (KeyStoreException e) {
  73. e.printStackTrace();
  74. } catch (CertificateException e) {
  75. e.printStackTrace();
  76. } catch (NoSuchAlgorithmException e) {
  77. e.printStackTrace();
  78. } catch (EOFException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. /**
  83.  * Fetches the password for a given account/user and server.
  84.  * @param user
  85.  * @param server
  86.  * @return <code>null</code> if no password could be obtained, the password 
  87.  *  otherwise
  88.  */
  89. public String getPassword(String user, String server) {
  90. try {
  91. KeyStore.SecretKeyEntry entry2 = (KeyStore.SecretKeyEntry) store
  92. .getEntry(user + "@" + server,
  93. new KeyStore.PasswordProtection(masterPassword));
  94. return new String(entry2.getSecretKey().getEncoded());
  95. } catch (KeyStoreException e) {
  96. e.printStackTrace();
  97. } catch (UnrecoverableEntryException ce) {
  98. ce.printStackTrace();
  99. } catch (NoSuchAlgorithmException ne) {
  100. ne.printStackTrace();
  101. }
  102. return null;
  103. }
  104. /**
  105.  * Adds a password to the KeyChain for a given account/user and server.
  106.  * 
  107.  * @param user
  108.  * @param server
  109.  * @param password
  110.  */
  111. public void addPassword(String user, String server, char[] password)
  112. {
  113. String pass = new String(password);
  114. SecretKeySpec passwordKey = new SecretKeySpec(pass.getBytes(), "JCEKS");
  115. KeyStore.SecretKeyEntry entry = new KeyStore.SecretKeyEntry(passwordKey);
  116. try {
  117. store.setEntry(user + "@" + server, entry,
  118. new KeyStore.PasswordProtection(masterPassword));
  119. } catch (KeyStoreException e) {
  120. e.printStackTrace();
  121. }
  122. }
  123. /**
  124.  * Removes a password for a given account/user and server.
  125.  * 
  126.  * @param user
  127.  * @param server
  128.  */
  129. public void removePassword(String user, String server) {
  130. try {
  131. store.deleteEntry(user + "@" + server);
  132. } catch (KeyStoreException e) {
  133. e.printStackTrace();
  134. }
  135. }
  136. /**
  137.  * Persists the KeyChain to an OutputStream
  138.  * 
  139.  * @param ostream
  140.  * @throws IOException
  141.  */
  142. public void store(OutputStream ostream) throws IOException {
  143. try {
  144. store.store(ostream, masterPassword);
  145. } catch (KeyStoreException e) {
  146. e.printStackTrace();
  147. } catch (CertificateException ce) {
  148. ce.printStackTrace();
  149. } catch (NoSuchAlgorithmException ne) {
  150. ne.printStackTrace();
  151. }
  152. }
  153. public static void main(String[] args) {
  154. try {
  155. File file = new File("c:\test.txt");
  156. FileInputStream fis;
  157. if (!file.exists()) {
  158. file.createNewFile();
  159. fis = null;
  160. } else {
  161. fis = new FileInputStream(file);
  162. }
  163. KeyChain kc = new KeyChain("test".toCharArray(), fis);
  164. kc.addPassword("bino", "sun-ds.sfbay", "test123".toCharArray());
  165. System.out.println("pass = "
  166. + kc.getPassword("bino", "sun-ds.sfbay"));
  167. System.out.println("More testing :");
  168. for (int i = 0; i < 100; i++) {
  169. kc.addPassword("" + i, "sun-ds.sfbay", ("" + i).toCharArray());
  170. }
  171. for (int i = 0; i < 100; i++) {
  172. System.out.println("key =" + i + " pass ="
  173. + kc.getPassword("" + i, "sun-ds.sfbay"));
  174. }
  175. kc.store(new FileOutputStream(file));
  176. } catch (Exception e) {
  177. e.printStackTrace();
  178. }
  179. }
  180. }