public static SaltedSecretKey createMasterKey(String password, byte[] salt) throws CipherException { try { logger.log(Level.FINE, "- Creating secret key using {0} with {1} rounds, key size {2} bit ...", new Object[] { MASTER_KEY_DERIVATION_FUNCTION, MASTER_KEY_DERIVATION_ROUNDS, MASTER_KEY_SIZE }); SecretKeyFactory factory = SecretKeyFactory.getInstance(MASTER_KEY_DERIVATION_FUNCTION); KeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, MASTER_KEY_DERIVATION_ROUNDS, MASTER_KEY_SIZE); SecretKey masterKey = factory.generateSecret(pbeKeySpec); return new SaltedSecretKey(masterKey, salt); } catch (Exception e) { throw new CipherException(e); } }
private static void writeExampleUserConfigFile(File userConfigFile) { UserConfigTO userConfigTO = new UserConfigTO(); try { System.out.println("First launch, creating a secret key (could take a sec)..."); SaltedSecretKey configEncryptionKey = CipherUtil.createMasterKey(CipherUtil.createRandomAlphabeticString(USER_CONFIG_ENCRYPTION_KEY_LENGTH)); userConfigTO.setConfigEncryptionKey(configEncryptionKey); userConfigTO.save(userConfigFile); } catch (CipherException e) { System.err.println("ERROR: " + e.getMessage()); System.err.println(" Failed to create masterkey."); System.err.println(); } catch (ConfigException e) { System.err.println("ERROR: " + e.getMessage()); System.err.println(" Failed to save to file."); System.err.println(); } }
private String decryptRepoFile(File file, SaltedSecretKey masterKey) throws CipherException { try { logger.log(Level.INFO, "Decrypting repo file ..."); FileInputStream encryptedRepoConfig = new FileInputStream(file); String repoFileStr = new String(CipherUtil.decrypt(encryptedRepoConfig, masterKey)); logger.log(Level.INFO, "Repo file decrypted:"); logger.log(Level.INFO, repoFileStr); return repoFileStr; } catch (Exception e) { logger.log(Level.INFO, "Invalid password given, or repo file corrupt.", e); throw new CipherException("Invalid password given, or repo file corrupt.", e); } }
public static void encrypt(InputStream plaintextInputStream, OutputStream ciphertextOutputStream, List<CipherSpec> cipherSpecs, SaltedSecretKey masterKey) throws CipherException { try { CipherSession cipherSession = new CipherSession(masterKey); OutputStream multiCipherOutputStream = new MultiCipherOutputStream(ciphertextOutputStream, cipherSpecs, cipherSession); int read = -1; byte[] buffer = new byte[4096]; while (-1 != (read = plaintextInputStream.read(buffer))) { multiCipherOutputStream.write(buffer, 0, read); } plaintextInputStream.close(); multiCipherOutputStream.close(); } catch (IOException e) { throw new CipherException(e); } }
throw new CipherException(e);
public static byte[] decrypt(InputStream fromInputStream, SaltedSecretKey masterKey) throws CipherException { try { CipherSession cipherSession = new CipherSession(masterKey); MultiCipherInputStream multiCipherInputStream = new MultiCipherInputStream(fromInputStream, cipherSession); ByteArrayOutputStream plaintextOutputStream = new ByteArrayOutputStream(); int read = -1; byte[] buffer = new byte[4096]; while (-1 != (read = multiCipherInputStream.read(buffer))) { plaintextOutputStream.write(buffer, 0, read); } multiCipherInputStream.close(); plaintextOutputStream.close(); return plaintextOutputStream.toByteArray(); } catch (IOException e) { throw new CipherException(e); } }
throw new CipherException("Unable to decrypt link.");