KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jmanage > core > tools > EncryptedKeyGenerator


1 /**
2  * Copyright 2004-2005 jManage.org
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.jmanage.core.tools;
17
18 import org.jmanage.core.auth.AuthConstants;
19 import org.jmanage.core.auth.User;
20 import org.jmanage.core.auth.UserManager;
21 import org.jmanage.core.config.*;
22 import org.jmanage.core.crypto.Crypto;
23 import org.jmanage.core.crypto.EncryptedKey;
24 import org.jmanage.core.crypto.KeyManager;
25 import org.jmanage.core.util.PasswordField;
26
27 import java.util.ArrayList JavaDoc;
28 import java.util.Arrays JavaDoc;
29 import java.util.Iterator JavaDoc;
30 import java.util.List JavaDoc;
31
32 /**
33  * Generates a symmetric key and encrypts it based on the given password.
34  *
35  * date: Jul 22, 2004
36  * @author Rakesh Kalra
37  */

38 public class EncryptedKeyGenerator {
39
40     public static void main(String JavaDoc[] args)
41         throws Exception JavaDoc{
42
43         /* display info */
44         message();
45         reencryptWithNewKey() ;
46     }
47
48     private static void message(){
49         System.out.println();
50         System.out.println("This tool generates a 128 bit TripleDES key and then");
51         System.out.println("encrypts it with Password Based Encryption (PBE),");
52         System.out.println("before writing it to jmanage-key file.");
53         System.out.println();
54     }
55
56     private static char[] getOldPassword()
57     throws Exception JavaDoc {
58           final char[] password = PasswordField.getPassword("Enter old password: ");
59           return password ;
60     }
61     private static char[] getPassword()
62         throws Exception JavaDoc {
63
64         final char[] password = PasswordField.getPassword("Enter new password:");
65         final char[] password2 = PasswordField.getPassword("Re-enter new password:");
66         if(!Arrays.equals(password, password2)){
67             System.out.println("Passwords do not match. " +
68                     "Key has not been generated.");
69             return null;
70         }
71         return password;
72     }
73
74     private static void reencryptWithNewKey( )
75         throws Exception JavaDoc {
76         ApplicationTypes.init();
77            /* get old password from user */
78        char [] oldPassword = getOldPassword() ;
79        UserManager userMgr = UserManager.getInstance() ;
80        User user = userMgr.verifyUsernamePassword(AuthConstants.USER_ADMIN,oldPassword);
81         if(user == null) {
82             System.out.println("\nInvalid password") ;
83             return ;
84         }
85          /* get new password from user */
86         char[] newPassword = getPassword();
87         if(newPassword == null){
88             return;
89         }
90
91           /* Get list of configured applications */
92         Crypto.init(oldPassword) ;
93         List JavaDoc configList = ConfigReader.getInstance().read() ;
94         if(configList == null) {
95             System.out.println("\nError in reading application passwords") ;
96             return ;
97         }
98
99         /* Write new key to file */
100       EncryptedKey encryptedKey = new EncryptedKey(newPassword);
101       KeyManager.writeKey(encryptedKey);
102       /* Change admin password */
103       UserManager.getInstance().deleteUser(AuthConstants.USER_ADMIN);
104       List JavaDoc roles = new ArrayList JavaDoc(1);
105       UserManager.getInstance().addUser(new User(AuthConstants.USER_ADMIN,
106       Crypto.hash(newPassword), roles, User.STATUS_ACTIVE, 0));
107       Crypto.init(newPassword);
108       ConfigWriter.getInstance().write(configList);
109       System.out.println("New key has been written to key file successfully..");
110     }
111 }
112
Popular Tags