1 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 ; 28 import java.util.Arrays ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 32 38 public class EncryptedKeyGenerator { 39 40 public static void main(String [] args) 41 throws Exception { 42 43 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 { 58 final char[] password = PasswordField.getPassword("Enter old password: "); 59 return password ; 60 } 61 private static char[] getPassword() 62 throws Exception { 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 { 76 ApplicationTypes.init(); 77 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 86 char[] newPassword = getPassword(); 87 if(newPassword == null){ 88 return; 89 } 90 91 92 Crypto.init(oldPassword) ; 93 List configList = ConfigReader.getInstance().read() ; 94 if(configList == null) { 95 System.out.println("\nError in reading application passwords") ; 96 return ; 97 } 98 99 100 EncryptedKey encryptedKey = new EncryptedKey(newPassword); 101 KeyManager.writeKey(encryptedKey); 102 103 UserManager.getInstance().deleteUser(AuthConstants.USER_ADMIN); 104 List roles = new ArrayList (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 |