/** * Decrypts the provided string. * * @param input * String (UTF-8 encoded) to be decrypted * @return The decrypted version of the input string, or directly the input string if no * encryption key is configured. */ public static String decrypt(String input) { if (encryptionService == null || input == null || !input.startsWith(ENCRYPTION_PREFIX)) { return input; } return encryptionService.decrypt(input.replaceFirst(ENCRYPTION_PREFIX, "")); }
/** * Encrypts the provided string. * * @param input * String (UTF-8 encoded) to be encrypted * @return The encrypted version of the input string, or directly the input string if no * encryption key is configured or the string is already encrypted. */ public static String encrypt(String input) { if (encryptionService == null || input == null || input.length() == 0 || input.startsWith(ENCRYPTION_PREFIX)) { return input; } return ENCRYPTION_PREFIX + encryptionService.encrypt(input); }
/** * Initializes (or re-initializes) the {@link EncryptorService} by reading the configured * encryption key file. */ public static void initEncryptionService() { File encryptionKey = getEncryptionFile(); if (encryptionKey == null) { encryptionService = null; } else { encryptionService = new EncryptorService(encryptionKey); try { encryptionService.encrypt(ENCRYPTION_PREFIX); } catch (Exception e) { throw new LocalizableValidationException(e, "Error validating the encryption key!", "common.encryption.file.validation"); } } }
/** * Encrypts the provided byte array. * * @param input * Byte array to be encrypted * @return The encrypted version of the input byte array (in base 64). */ public byte[] encrypt(final byte[] input) { if (input == null || input.length == 0) { return input; } try { BufferedBlockCipher cipher = getCipher(true); byte[] output = new byte[cipher.getOutputSize(input.length)]; int length = cipher.processBytes(input, 0, input.length, output, 0); length += cipher.doFinal(output, length); return Base64.getEncoder().encode(Arrays.copyOfRange(output, 0, length)); } catch (Exception e) { throw new LocalizableValidationException(e, "Encryption error!", "common.ecryption.error"); } }
private static File getEncryptionFile() { String param = System.getProperty(ENCRYPTION_KEY); if (param == null) { return null; } File encryptionKeyFile = new File(param); if (!encryptionKeyFile.exists()) { if (Boolean.getBoolean(INIT_KEY_IF_MISSING)) { try { Files.write(encryptionKeyFile.toPath(), EncryptorService.generateKey()); } catch (Exception e) { throw new LocalizableValidationException(e, "Error initializing the encryption key file '" + param + "'!", "common.encryption.file.init", param); } } else { throw new LocalizableValidationException("File '" + param + "' does not exist!", "common.encryption.file.missing", param); } } return encryptionKeyFile; }
/** * Initializes (or re-initializes) the {@link EncryptorService} by reading the configured * encryption key file. */ public static void initEncryptionService() { File encryptionKey = getEncryptionFile(); if (encryptionKey == null) { encryptionService = null; } else { encryptionService = new EncryptorService(encryptionKey); try { encryptionService.encrypt(ENCRYPTION_PREFIX); } catch (Exception e) { throw new LocalizableValidationException(e, "Error validating the encryption key!", "common.encryption.file.validation"); } } }
/** * Encrypts the provided byte array. * @param input * Byte array to be encrypted * @return The encrypted version of the input byte array (in base 64). */ public byte[] encrypt(final byte[] input) { if (input == null || input.length == 0) { return input; } try { BufferedBlockCipher cipher = getCipher(true); byte[] output = new byte[cipher.getOutputSize(input.length)]; int length = cipher.processBytes(input, 0, input.length, output, 0); length += cipher.doFinal(output, length); return Base64.getEncoder().encode(Arrays.copyOfRange(output, 0, length)); } catch (Exception e) { throw new LocalizableValidationException(e, "Encryption error!", "common.ecryption.error"); } }
private static File getEncryptionFile() { String param = System.getProperty(ENCRYPTION_KEY); if (param == null) { return null; } File encryptionKeyFile = new File(param); if (!encryptionKeyFile.exists()) { if (Boolean.getBoolean(INIT_KEY_IF_MISSING)) { try { Files.write(encryptionKeyFile.toPath(), EncryptorService.generateKey()); } catch (Exception e) { throw new LocalizableValidationException(e, "Error initializing the encryption key file '" + param + "'!", "common.encryption.file.init", param); } } else { throw new LocalizableValidationException("File '" + param + "' does not exist!", "common.encryption.file.missing", param); } } return encryptionKeyFile; }
/** * Decrypts the provided string. * * @param input * String (UTF-8 encoded) to be decrypted * @return The decrypted version of the input string, or directly the input string if no * encryption key is configured. */ public static String decrypt(String input) { if (encryptionService == null || input == null || !input.startsWith(ENCRYPTION_PREFIX)) { return input; } return encryptionService.decrypt(input.replaceFirst(ENCRYPTION_PREFIX, "")); }
/** * Encrypts the provided string. * @param input * String (UTF-8 encoded) to be encrypted * @return The encrypted version of the input string. */ public String encrypt(String input) { if (input == null || input.length() == 0) { return input; } byte[] inputBytes = input.getBytes(UTF_8); byte[] outputBytes = encrypt(inputBytes); return new String(outputBytes, UTF_8); }
/** * Decrypts the provided byte array. * @param input * Byte array (in base 64) to be decrypted * @return The decrypted version of the input byte array. */ public byte[] decrypt(final byte[] input) { if (input == null || input.length == 0) { return input; } try { BufferedBlockCipher cipher = getCipher(false); byte[] bytes = Base64.getDecoder().decode(input); byte[] output = new byte[cipher.getOutputSize(bytes.length)]; int length = cipher.processBytes(bytes, 0, bytes.length, output, 0); length += cipher.doFinal(output, length); return Arrays.copyOfRange(output, 0, length); } catch (Exception e) { throw new LocalizableValidationException(e, "Decryption error!", "common.dercyption.error"); } }
/** * Decrypts the provided string. * @param input * String (UTF-8 encoded) to be decrypted * @return The decrypted version of the input string. */ public String decrypt(String input) { if (input == null || input.length() == 0) { return input; } byte[] inputBytes = input.getBytes(UTF_8); byte[] outputBytes = decrypt(inputBytes); return new String(outputBytes, UTF_8); }
/** * Encrypts the provided string. * * @param input * String (UTF-8 encoded) to be encrypted * @return The encrypted version of the input string, or directly the input string if no * encryption key is configured or the string is already encrypted. */ public static String encrypt(String input) { if (encryptionService == null || input == null || input.length() == 0 || input.startsWith(ENCRYPTION_PREFIX)) { return input; } return ENCRYPTION_PREFIX + encryptionService.encrypt(input); }
/** * Decrypts the provided byte array. * * @param input * Byte array (in base 64) to be decrypted * @return The decrypted version of the input byte array. */ public byte[] decrypt(final byte[] input) { if (input == null || input.length == 0) { return input; } try { BufferedBlockCipher cipher = getCipher(false); byte[] bytes = Base64.getDecoder().decode(input); byte[] output = new byte[cipher.getOutputSize(bytes.length)]; int length = cipher.processBytes(bytes, 0, bytes.length, output, 0); length += cipher.doFinal(output, length); return Arrays.copyOfRange(output, 0, length); } catch (Exception e) { throw new LocalizableValidationException(e, "Decryption error!", "common.dercyption.error"); } }
/** * Decrypts the provided string. * * @param input * String (UTF-8 encoded) to be decrypted * @return The decrypted version of the input string. */ public String decrypt(String input) { if (input == null || input.length() == 0) { return input; } byte[] inputBytes = input.getBytes(UTF_8); byte[] outputBytes = decrypt(inputBytes); return new String(outputBytes, UTF_8); }
/** * Encrypts the provided string. * * @param input * String (UTF-8 encoded) to be encrypted * @return The encrypted version of the input string. */ public String encrypt(String input) { if (input == null || input.length() == 0) { return input; } byte[] inputBytes = input.getBytes(UTF_8); byte[] outputBytes = encrypt(inputBytes); return new String(outputBytes, UTF_8); }