1   package org.wcb.gui.util;
2   
3   
4   import javax.crypto.SecretKey;
5   import javax.crypto.SecretKeyFactory;
6   import javax.crypto.Cipher;
7   import javax.crypto.spec.PBEKeySpec;
8   import javax.crypto.spec.PBEParameterSpec;
9   import java.util.logging.Logger;
10  import java.util.logging.Level;
11  import java.security.GeneralSecurityException;
12  
13  /**
14   * <small>
15   * <p>
16   * Copyright (c)  2006  wbogaardt.
17   * This library is free software; you can redistribute it and/or
18   * modify it under the terms of the GNU Lesser General Public
19   * License as published by the Free Software Foundation; either
20   * version 2.1 of the License, or (at your option) any later version.
21   *
22   * This library is distributed in the hope that it will be useful,
23   * but WITHOUT ANY WARRANTY; without even the implied warranty of
24   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
25   * Lesser General Public License for more details.
26   *
27   * You should have received a copy of the GNU Lesser General Public
28   * License along with this library; if not, write to the Free Software
29   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
30   * <p/>
31   * $File:  $ <br>
32   * $Change:  $ submitted by $Author: wbogaardt $ at $DateTime: Sep 26, 2006 11:44:35 AM $ <br>
33   * </small>
34   *
35   * @author wbogaardt
36   *         Authentication
37   */
38  
39  public class AuthenticationUtility {
40  
41  
42      private String username;
43      private String password;
44      private boolean rememberName;
45      private Logger LOG = Logger.getLogger(AuthenticationUtility.class.getName());
46      
47      /**
48       * Name of the algorithm used for encryption\decryption.
49       */
50      private static final String ALGORITHM = "PBEWithMD5AndDES";
51  
52      /**
53       * Password for Key
54       */
55      private static final char[] KEY_PASSWORD = "pilotlogbook".toCharArray();
56      private static final String USERNAME = "username";
57      private static final String PASSWORD = "password";
58      private static final String REMEMBER_NAME = "username.rembmer";
59  
60      public AuthenticationUtility() {
61          load();
62      }
63  
64      private void load() {
65          username = ApplicationPreferences.getInstance().getString(USERNAME);
66          password = ApplicationPreferences.getInstance().getString(PASSWORD);
67          rememberName = ApplicationPreferences.getInstance().getBoolean(REMEMBER_NAME);
68      }
69  
70      /**
71       * Should only be used to clear user name and password
72       * in cases of unit testing.
73       */
74      public void clearAuthentication() {
75          ApplicationPreferences.getInstance().removeKey(USERNAME);
76          ApplicationPreferences.getInstance().removeKey(PASSWORD);
77      }
78  
79      public void createNewUser(String user, String passwrd) {
80          this.setUsername(user);
81          this.setPassword(encrypt(passwrd));
82          ApplicationPreferences.getInstance().putString(USERNAME, username);
83          ApplicationPreferences.getInstance().putString(PASSWORD, encrypt(passwrd));
84          /*Preferences prefs = Preferences.userNodeForPackage(AuthenticationUtility.class);
85          prefs.put(USERNAME, username);
86          prefs.put(PASSWORD, encrypt(passwrd));*/
87      }
88  
89      public boolean authenticateUser(String user, String passwrd) {
90          String unencryptPassword = decrypt(password);
91          return user.equalsIgnoreCase(username) && passwrd.equalsIgnoreCase(unencryptPassword);
92      }
93  
94      public boolean updatePassword(String oldpasswrd, String passwrd) {
95          String unencryptPassword = decrypt(password);
96          if(unencryptPassword.equalsIgnoreCase(oldpasswrd) || username.equals("")) {
97              this.setPassword(encrypt(passwrd));
98              ApplicationPreferences.getInstance().putString(USERNAME, username);
99              ApplicationPreferences.getInstance().putString(PASSWORD, encrypt(passwrd));
100             /*Preferences prefs = Preferences.userNodeForPackage(AuthenticationUtility.class);
101             prefs.put(USERNAME, username);
102             prefs.put(PASSWORD, encrypt(passwrd));*/
103             return true;
104         }
105         return false;
106     }
107 
108     public String encrypt(String pass) {
109         Cipher cipher;
110         SecretKey secretKey;
111         PBEParameterSpec pbeParmSpec;
112         byte[] encData = null;
113         try {
114             // create the key
115             secretKey = createSecretKey();
116 
117             // create the PBEParameterSpec
118             pbeParmSpec = createParameterSpec();
119 
120             // create and initialize the Cipher
121             // using the secret key, and param spec
122             cipher = Cipher.getInstance(ALGORITHM);
123             cipher.init(Cipher.ENCRYPT_MODE, secretKey, pbeParmSpec);
124 
125             // encrypt the data
126             encData = cipher.doFinal(pass.getBytes());
127 
128             // catch an exceptions that may occur
129         } catch (GeneralSecurityException gse) {
130             LOG.log(Level.WARNING, gse.getMessage());
131         }
132         return new String(encData);
133     }
134 
135     public String decrypt(String newPassword) {
136         Cipher cipher;
137         SecretKey secretKey;
138         PBEParameterSpec pbeParmSpec;
139         byte[] decData = null;
140         try {
141             // create the key
142             secretKey = createSecretKey();
143 
144             // create the PBEParameterSpec
145             pbeParmSpec = createParameterSpec();
146 
147             // create and initialize the Cipher
148             // using the secret key, and param spec
149             cipher = Cipher.getInstance(ALGORITHM);
150             cipher.init(Cipher.DECRYPT_MODE, secretKey, pbeParmSpec);
151 
152             // decrypt the data
153             decData = cipher.doFinal(newPassword.getBytes());
154 
155 
156 
157             // catch an exceptions that may occur
158         } catch (GeneralSecurityException gse) {
159             LOG.log(Level.WARNING, gse.getMessage(), gse);
160         }
161         return new String(decData);
162     }
163 
164     public String getUsername() {
165         return username;
166     }
167 
168     public void setUsername(String username) {
169         this.username = username;
170     }
171 
172     public String getPassword() {
173         return password;
174     }
175 
176     public void setPassword(String password) {
177         this.password = password;
178     }
179 
180     /**
181      * Set to true to remeber the user name on the login form
182      * @param val Set to true to remember name on user form
183      */
184     public void setRememberUsername(boolean val) {
185         this.rememberName = val;
186         ApplicationPreferences.getInstance().setBoolean(REMEMBER_NAME, val);
187     }
188 
189     /**
190      * True indicates user wants to have the login dialog remember their username.
191      * @return  true remember username.
192      */
193     public boolean isRememberName() {
194         return this.rememberName;
195     }
196 
197     /**
198      * Returns the SecretKey used for encryption and decryption.
199      *
200      * @return SecretKey - the SecretKey
201      * @throws java.security.GeneralSecurityException - on error;
202      */
203     private SecretKey createSecretKey() throws GeneralSecurityException {
204         // tmp variables
205         PBEKeySpec pbeKeySpec;
206         SecretKeyFactory skFactory;
207 
208         // create SecretKey
209         pbeKeySpec = new PBEKeySpec(KEY_PASSWORD);
210         skFactory = SecretKeyFactory.getInstance(ALGORITHM);
211 
212         // return it
213         return skFactory.generateSecret(pbeKeySpec);
214     }
215 
216     /**
217      * Returns the PBEParameterSpec used for initializing
218      * a Cipher object.
219      *
220      * @return PBEParameterSpec - the PBEParameterSpec.
221      */
222     private PBEParameterSpec createParameterSpec() {
223         return new PBEParameterSpec(("LMBSALT!".getBytes()), 50);
224     }
225 }