/** * Creates a new instance that will use the given passphrase for all encryption/decryption activities. * * @param passphrase the passphrase to encrypt/decrypt the credentials with * @return the new instance */ public static CredentialsEncryptor withPassphrase(char[] passphrase) { Encryption encryption = Encryption.createEncryption(passphrase); return new CredentialsEncryptor(encryption); }
/** * Encrypts the given string. * * @param string the string to encrypt * @return the encrypted string */ public String encrypt(String string) { return string != null ? encryption.encrypt(string) : null; }
/** * Decrypts the given string. * * @param string the string to decrypt * @return the decrypted string */ public String decrypt(String string) { return string != null ? encryption.decrypt(string) : null; }
@Test public void encryptDecryptAcrossDifferentEncryptionInstances() { char[] passphrase = "My very secret pass phrase".toCharArray(); String textToEncrypt = "Some text that needs to be encrypted."; Encryption encryption = Encryption.createEncryption(passphrase); String encrypted = encryption.encrypt(textToEncrypt); Encryption encryption2 = Encryption.createEncryption(passphrase); String decrypted = encryption2.decrypt(encrypted); assertEquals(textToEncrypt, decrypted); }
public OrderedProperties readCredentials() { OrderedProperties credentials = createOrderedProperties(); // read the file with the encrypted credentials, if it already exists File file = getCredentialsFile(); if (file.exists()) { LOGGER.debug("Read existing credentials file: " + file.getAbsolutePath()); loadProperties(credentials, file); } else { LOGGER.debug("Credentials file does not exist yet: " + file.getAbsolutePath()); } return credentials; }
@TaskAction void addCredentials() throws IOException { // get credentials key and value from the project properties String key = getCredentialsKey(); String value = getCredentialsValue(); char[] placeholderValue = new char[value.length()]; Arrays.fill(placeholderValue, '*'); LOGGER.debug(String.format("Add credentials with key: '%s', value: '%s'", key, new String(placeholderValue))); // read the current persisted credentials OrderedProperties credentials = credentialsPersistenceManager.readCredentials(); // encrypt value and update credentials String encryptedValue = credentialsEncryptor.encrypt(value); credentials.setProperty(key, encryptedValue); // persist the updated credentials credentialsPersistenceManager.storeCredentials(credentials); }
public void storeCredentials(OrderedProperties credentials) { // write the updated credentials File file = getCredentialsFile(); LOGGER.debug("Write updated credentials file: " + file.getAbsolutePath()); saveProperties(credentials, file); }
@OutputFile public File getEncryptedPropertiesFile() { return credentialsPersistenceManager.getCredentialsFile(); }
private static void loadProperties(OrderedProperties properties, File file) { try { FileInputStream inputStream = new FileInputStream(file); try { loadProperties(properties, inputStream); } finally { inputStream.close(); } } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * Creates a new Encryption instance that uses password-based encryption (PBE). The algorithm used to create the secret key is <i>PBEWithMD5AndDES</i>. * * @param passphrase the passphrase to apply when creating the secret key * @return the new Encryption instance * @throws RuntimeException with wrapped GeneralSecurityException in case of crypto-related exceptions */ public static Encryption createEncryption(char[] passphrase) { try { return createEncryptionThrowingException(passphrase); } catch (GeneralSecurityException e) { throw new RuntimeException("Cannot create Encryption instance: " + e.getMessage(), e); } }
public OrderedProperties readCredentials() { OrderedProperties credentials = createOrderedProperties(); // read the file with the encrypted credentials, if it already exists File file = getCredentialsFile(); if (file.exists()) { LOGGER.debug("Read existing credentials file: " + file.getAbsolutePath()); loadProperties(credentials, file); } else { LOGGER.debug("Credentials file does not exist yet: " + file.getAbsolutePath()); } return credentials; }
/** * Creates a new instance that will use the given passphrase for all encryption/decryption activities. * * @param passphrase the passphrase to encrypt/decrypt the credentials with * @return the new instance */ public static CredentialsEncryptor withPassphrase(char[] passphrase) { Encryption encryption = Encryption.createEncryption(passphrase); return new CredentialsEncryptor(encryption); }
public void storeCredentials(OrderedProperties credentials) { // write the updated credentials File file = getCredentialsFile(); LOGGER.debug("Write updated credentials file: " + file.getAbsolutePath()); saveProperties(credentials, file); }
/** * Encrypts the given string. * * @param string the string to encrypt * @return the encrypted string */ public String encrypt(String string) { return string != null ? encryption.encrypt(string) : null; }
/** * Decrypts the given string. * * @param string the string to decrypt * @return the decrypted string */ public String decrypt(String string) { return string != null ? encryption.decrypt(string) : null; }
@OutputFile public File getEncryptedPropertiesFile() { return credentialsPersistenceManager.getCredentialsFile(); }
private static void loadProperties(OrderedProperties properties, File file) { try { FileInputStream inputStream = new FileInputStream(file); try { loadProperties(properties, inputStream); } finally { inputStream.close(); } } catch (IOException e) { throw new UncheckedIOException(e); } }
/** * Creates a new Encryption instance that uses password-based encryption (PBE). The algorithm used to create the secret key is <i>PBEWithMD5AndDES</i>. * * @param passphrase the passphrase to apply when creating the secret key * @return the new Encryption instance * @throws RuntimeException with wrapped GeneralSecurityException in case of crypto-related exceptions */ public static Encryption createEncryption(char[] passphrase) { try { return createEncryptionThrowingException(passphrase); } catch (GeneralSecurityException e) { throw new RuntimeException("Cannot create Encryption instance: " + e.getMessage(), e); } }
@OutputFile public File getEncryptedPropertiesFile() { return credentialsPersistenceManager.getCredentialsFile(); }
@OutputFile public File getEncryptedPropertiesFile() { return credentialsPersistenceManager.getCredentialsFile(); }