protected String getRandomMachineName() { return CipherUtil.createRandomAlphabeticString(20); }
private SaltedSecretKey createSaltedSecretKey(CipherSpec cipherSpec, byte[] salt) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException { return CipherUtil.createDerivedKey(masterKey, salt, cipherSpec); }
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(); } }
public static SaltedSecretKey createMasterKey(String password) throws CipherException { byte[] salt = createRandomArray(MASTER_KEY_SALT_SIZE / 8); return createMasterKey(password, salt); }
private void testEncrypt(byte[] originalData, List<CipherSpec> cipherSpecs) throws CipherException { SaltedSecretKey masterKey = createDummyMasterKey(); byte[] ciphertext = CipherUtil.encrypt( new ByteArrayInputStream(originalData), cipherSpecs, masterKey ); byte[] plaintext = CipherUtil.decrypt(new ByteArrayInputStream(ciphertext), masterKey); assertFalse(Arrays.equals(originalData, ciphertext)); assertTrue(Arrays.equals(originalData, plaintext)); }
private SaltedSecretKey createMasterKeyFromPassword(String masterPassword, byte[] masterKeySalt) throws CipherException { fireNotifyCreateMaster(); SaltedSecretKey masterKey = CipherUtil.createMasterKey(masterPassword, masterKeySalt); return masterKey; }
/** * Loads the repository transfer object from the local directory. */ public static RepoTO loadRepoTO(File localDir, ConfigTO configTO) throws ConfigException { File appDir = new File(localDir, Config.DIR_APPLICATION); File repoFile = new File(appDir, Config.FILE_REPO); if (!repoFile.exists()) { throw new ConfigException("Cannot find repository file at "+repoFile+". Try connecting to a repository using 'connect', or 'init' to create a new one."); } try { if (CipherUtil.isEncrypted(repoFile)) { return loadEncryptedRepoTO(repoFile, configTO); } else { return loadPlaintextRepoTO(repoFile, configTO); } } catch (Exception e) { throw new ConfigException("Cannot load repo file: "+e.getMessage(), e); } }
public static byte[] encrypt(InputStream plaintextInputStream, List<CipherSpec> cipherSuites, SaltedSecretKey masterKey) throws CipherException { ByteArrayOutputStream ciphertextOutputStream = new ByteArrayOutputStream(); encrypt(plaintextInputStream, ciphertextOutputStream, cipherSuites, masterKey); return ciphertextOutputStream.toByteArray(); }
private SaltedSecretKey createSaltedSecretKey(CipherSpec cipherSpec) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException { byte[] salt = CipherUtil.createRandomArray(MultiCipherOutputStream.SALT_SIZE); return createSaltedSecretKey(cipherSpec, salt); }
public TransferSettings createTransferSettings(SaltedSecretKey masterKey) throws Exception { if (!encrypted || encryptedSettingsBytes == null) { throw new IllegalArgumentException("Link is not encrypted. Cannot call this method."); } byte[] plaintextPluginSettingsBytes = CipherUtil.decrypt(new ByteArrayInputStream(encryptedSettingsBytes), masterKey); return createTransferSettings(plaintextPluginSettingsBytes); }
private String calculateChecksum(File tempPluginJarFile) throws Exception { CipherUtil.enableUnlimitedStrength(); byte[] actualChecksum = FileUtil.createChecksum(tempPluginJarFile, "SHA256"); return StringUtil.toHex(actualChecksum); }
public static SSLContext createUserSSLContext() throws Exception { return CipherUtil.createSSLContext(userKeyStore, userTrustStore); }
@Test(expected = CipherException.class) public void testIntegrityAesGcmCiphertext() throws Exception { SaltedSecretKey masterKey = createDummyMasterKey(); byte[] originalPlaintext = TestFileUtil.createRandomArray(50); byte[] ciphertext = CipherUtil.encrypt( new ByteArrayInputStream(originalPlaintext), Arrays.asList(CipherSpecs.getCipherSpec(CipherSpecs.AES_128_GCM)), masterKey ); // Alter ciphertext (after header!); ciphertext starts after 75 bytes ciphertext[80] = (byte) (ciphertext[80] ^ 0x01); ciphertext[81] = (byte) (ciphertext[81] ^ 0x02); ciphertext[82] = (byte) (ciphertext[82] ^ 0x03); CipherUtil.decrypt(new ByteArrayInputStream(ciphertext), masterKey); fail("TEST FAILED: Ciphertext was altered without exception."); }
private static SaltedSecretKey getMasterKey() throws Exception { if (!cryptoEnabled) { return null; } else { if (masterKey == null) { masterKey = CipherUtil.createMasterKey("some password"); } return masterKey; } }
@Test public void testIsEncryptedFileTrue() throws Exception { File tempDirectory = TestFileUtil.createTempDirectoryInSystemTemp(); File testFile = new File(tempDirectory+"/somefile"); RandomAccessFile testFileRaf = new RandomAccessFile(testFile, "rw"); testFileRaf.write(MultiCipherOutputStream.STREAM_MAGIC); testFileRaf.write(MultiCipherOutputStream.STREAM_VERSION); testFileRaf.close(); assertTrue(CipherUtil.isEncrypted(testFile)); TestFileUtil.deleteDirectory(tempDirectory); }
public void save(File file, List<CipherSpec> cipherSpecs, SaltedSecretKey masterKey) throws ConfigException { try { ByteArrayOutputStream plaintextRepoOutputStream = new ByteArrayOutputStream(); Serializer serializer = new Persister(); serializer.write(this, plaintextRepoOutputStream); CipherUtil.encrypt(new ByteArrayInputStream(plaintextRepoOutputStream.toByteArray()), new FileOutputStream(file), cipherSpecs, masterKey); } catch (Exception e) { throw new ConfigException("Cannot write repoTO (encrypted) to file " + file, e); } }
@Test public void testCreateRandomArray() throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchProviderException { byte[] randomArray1 = CipherUtil.createRandomArray(10); byte[] randomArray2 = CipherUtil.createRandomArray(10); assertEquals(10, randomArray1.length); assertEquals(10, randomArray2.length); assertFalse(Arrays.equals(randomArray1, randomArray2)); }
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); } }
CipherUtil.enableUnlimitedStrength();
@Test(expected = Exception.class) public void testIntegrityTwofishGcmCiphertext() throws Exception { SaltedSecretKey masterKey = createDummyMasterKey(); byte[] originalPlaintext = TestFileUtil.createRandomArray(50); byte[] ciphertext = CipherUtil.encrypt( new ByteArrayInputStream(originalPlaintext), Arrays.asList(CipherSpecs.getCipherSpec(CipherSpecs.TWOFISH_128_GCM)), masterKey ); // Alter ciphertext (after header!); ciphertext starts after 75 bytes ciphertext[80] = (byte) (ciphertext[80] ^ 0x01); byte[] plaintext = CipherUtil.decrypt(new ByteArrayInputStream(ciphertext), masterKey); System.out.println(StringUtil.toHex(originalPlaintext)); System.out.println(StringUtil.toHex(plaintext)); fail("TEST FAILED: Ciphertext was altered without exception."); }