Java tutorial
package my.jabbr.app.ftpclient.controller; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; import java.security.PrivateKey; import java.security.PublicKey; import java.util.Map.Entry; import java.util.Set; import my.jabbr.app.ftpclient.exceptions.AppNotInstalledException; import my.jabbr.app.ftpclient.ui.JabbrFTPClient; import my.jabbr.crypto.abe.api.ABEPublicKey; import my.jabbr.crypto.abe.exceptions.EncryptionAlgorithmMismatchException; import my.jabbr.crypto.abe.exceptions.InvalidABEFileFormatException; import my.jabbr.crypto.utils.JabbrKeyStoreUtils; import my.jabbr.crypto.utils.rsa.InvalidRSAKeyFormatException; import my.jabbr.crypto.utils.rsa.RSAKeyStoreUtils; import org.apache.commons.io.FileUtils; public class ConfigManager { private JabbrFTPClient client; private static ConfigManager INSTANCE = null; private final static File CONFIGURATION_FILE = new File( System.getProperty("user.home") + "/" + ".jabbrftp" + "/" + "jabbrftp.conf"); private final String PUBLIC_ENCRYPTION_KEY_FILE = "elements.pub_key"; private final String USER_ENCRYPTION_KEY_FILE = "user.user_key"; private final String PROXY_ENCRYPTION_KEY_FILE = "user.proxy_key"; private final String PUBLIC_SIGNING_KEY_FILE = "RSA_PUBLIC_KEY.rsa_key"; private final String SECRET_SIGNING_KEY_FILE = "RSA_PRIV_KEY.rsa_key"; private final String DEFAULT_WORKSPACE_DIRECTORY = System.getProperty("user.home").replace('\\', '/') + "/" + "JabbrFiles"; public static final int MULTIVALUED_DEFAULT_COUNT = 2; public static final String VAL_NOT_SELECTED = "NO"; public static final String GPL_LICENSE_FILE = "license/gpl-3.0.txt"; public static final String VAL_SELECTED = "Yes"; private ConfigManager(JabbrFTPClient client) { this.client = client; } public static ConfigManager getInstance(JabbrFTPClient client) { if (INSTANCE == null) { INSTANCE = new ConfigManager(client); } return INSTANCE; } public void prepareHome() { new AppConfiguration(); } public AppConfiguration createAppConfiguration(String schemeCode) { AppConfiguration config = new AppConfiguration(); config.setEncryptionScheme(schemeCode); config.setPublicEncryptionKeyFile(config.getAbeHome() + "/" + PUBLIC_ENCRYPTION_KEY_FILE); config.setUserSecretEncryptionKeyFile(config.getAbeHome() + "/" + USER_ENCRYPTION_KEY_FILE); config.setProxySecretEncryptionKeyFile(config.getAbeHome() + "/" + PROXY_ENCRYPTION_KEY_FILE); config.setPublicSigningKeyFile(config.getRsaHome() + "/" + PUBLIC_SIGNING_KEY_FILE); config.setSecretSigningKeyFile(config.getRsaHome() + "/" + SECRET_SIGNING_KEY_FILE); config.setWorkspaceDirectory(DEFAULT_WORKSPACE_DIRECTORY); updateAppConfig(config); return config; } public AppConfiguration loadConfigurationFile() throws AppNotInstalledException { AppConfiguration config = new AppConfiguration(); BufferedReader in = null; try { in = new BufferedReader(new FileReader(CONFIGURATION_FILE)); String line = null; while ((line = in.readLine()) != null) { String[] property = line.split("="); if (property.length == 2) { config.put(property[0], property[1]); } else { config.put(property[0], null); } } } catch (FileNotFoundException e) { throw new AppNotInstalledException(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (in != null) in.close(); } catch (IOException e) { e.printStackTrace(); } } return config; } public void updateAppConfig(AppConfiguration config) { PrintWriter out = null; try { out = new PrintWriter(CONFIGURATION_FILE); Set<Entry<String, String>> entrySet = config.entrySet(); for (Entry<String, String> entry : entrySet) { out.println(entry.getKey() + "=" + entry.getValue()); } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (out != null) { out.close(); } } } public void copyABEKeys(String defaultScheme, String sourcePKFile, String sourceASKFile, char[] password) throws IOException, EncryptionAlgorithmMismatchException, InvalidABEFileFormatException { AppConfiguration appConfig = client.getAppConfig(); try { ABEPublicKey pk = JabbrKeyStoreUtils.readPublicKeyFromFile(defaultScheme, sourcePKFile); JabbrKeyStoreUtils.writeToFile(pk, appConfig.getPublicEncryptionKeyFile(), defaultScheme); byte[] plaintextSecretKeyBytes = FileUtils.readFileToByteArray(new File(sourceASKFile)); JabbrKeyStoreUtils.pbeEncryptBytes(plaintextSecretKeyBytes, password, appConfig.getUserSecretEncryptionKeyFile()); //ABEUserAttributeSet userAttributes = ABEObjectFactory.createUserAttributeSetObj(); //ABESecretKey userKey = JabbrKeyStoreUtils.readUserKeyFromFile(defaultScheme, userAttributes, pk, sourceASKFile); //JabbrKeyStoreUtils.writeToFile(userKey, appConfig.getUserSecretEncryptionKeyFile(), defaultScheme, userAttributes); } catch (NumberFormatException e) { e.printStackTrace(); } } public void copyRSAKeys(String sourcePK, String sourcePRK, char[] password) throws IOException, InvalidRSAKeyFormatException { PublicKey pubKey = RSAKeyStoreUtils.readPublicKeyFromFile(sourcePK); PrivateKey privKey = RSAKeyStoreUtils.readPrivateKeyFromFile(sourcePRK); AppConfiguration appConfig = client.getAppConfig(); RSAKeyStoreUtils.storeKeyToFile(new File(appConfig.getPublicSigningKeyFile()), pubKey); RSAKeyStoreUtils.pbeEncryptRSAPrivateKey(privKey, password, appConfig.getSecretSigningKeyFile()); //RSAKeyStoreUtils.storeKeyToFile(new File(appConfig.getSecretSigningKeyFile()), privKey); } }