1 package org.jdesktop.swing.auth; 2 3 9 10 import java.io.EOFException ; 11 import java.io.File ; 12 import java.io.FileInputStream ; 13 import java.io.FileOutputStream ; 14 import java.io.IOException ; 15 import java.io.InputStream ; 16 import java.io.OutputStream ; 17 import java.security.KeyStore ; 18 import java.security.KeyStoreException ; 19 import java.security.NoSuchAlgorithmException ; 20 import java.security.UnrecoverableEntryException ; 21 import java.security.cert.CertificateException ; 22 23 import javax.crypto.spec.SecretKeySpec; 24 25 45 public class KeyChain { 46 private KeyStore store; 47 48 private char[] masterPassword; 49 50 58 public KeyChain(char[] masterPassword, InputStream inputStream) 59 throws IOException { 60 this.masterPassword = masterPassword; 61 62 try { 63 store = KeyStore.getInstance("JCEKS"); 64 store.load(inputStream, masterPassword); 65 66 } catch (KeyStoreException e) { 67 e.printStackTrace(); 68 } catch (CertificateException e) { 69 e.printStackTrace(); 70 } catch (NoSuchAlgorithmException e) { 71 e.printStackTrace(); 72 } catch (EOFException e) { 73 e.printStackTrace(); 74 } 75 76 } 77 78 84 public String getPassword(String user, String server) { 85 86 try { 87 88 KeyStore.SecretKeyEntry entry2 = (KeyStore.SecretKeyEntry ) store 89 .getEntry(user + "@" + server, 90 new KeyStore.PasswordProtection (masterPassword)); 91 return new String (entry2.getSecretKey().getEncoded()); 92 } catch (KeyStoreException e) { 93 e.printStackTrace(); 94 } catch (UnrecoverableEntryException ce) { 95 ce.printStackTrace(); 96 } catch (NoSuchAlgorithmException ne) { 97 ne.printStackTrace(); 98 } 99 100 return null; 101 } 102 103 111 public void addPassword(String user, String server, char[] password) 112 throws IOException { 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 130 public void removePassword(String user, String server) { 131 try { 132 store.deleteEntry(user + "@" + server); 133 } catch (KeyStoreException e) { 134 e.printStackTrace(); 135 } 136 } 137 138 144 145 public void store(OutputStream ostream) throws IOException { 146 try { 147 store.store(ostream, masterPassword); 148 } catch (KeyStoreException e) { 149 e.printStackTrace(); 150 } catch (CertificateException ce) { 151 ce.printStackTrace(); 152 } catch (NoSuchAlgorithmException ne) { 153 ne.printStackTrace(); 154 } 155 } 156 157 158 public static void main(String [] args) { 159 try { 160 File file = new File ("c:\\test.txt"); 161 FileInputStream fis; 162 if (!file.exists()) { 163 file.createNewFile(); 164 fis = null; 165 } else { 166 fis = new FileInputStream (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 (file)); 182 } catch (Exception e) { 183 e.printStackTrace(); 184 } 185 } 186 187 } 188 | Popular Tags |