public String decode(final String encodedValue, final CipherAlgorithm cipherAlgorithm) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { String value = null; if (encodedValue != null && cipherAlgorithm == CipherAlgorithm.AES) { final byte[] encoded = encodedValue.getBytes(StandardCharsets.UTF_8); final Cipher cipher = Cipher.getInstance(CipherAlgorithm.AES.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, keySpec); value = new String(cipher.doFinal(Base64.getDecoder().decode(encoded)), StandardCharsets.UTF_8); } return value; }
public String decode(final String encodedValue, final CipherAlgorithm cipherAlgorithm) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { String value = null; if (encodedValue != null && cipherAlgorithm == CipherAlgorithm.AES) { final byte[] encoded = encodedValue.getBytes(StandardCharsets.UTF_8); final Cipher cipher = Cipher.getInstance(CipherAlgorithm.AES.getAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, keySpec); value = new String(cipher.doFinal(Base64.getDecoder().decode(encoded)), StandardCharsets.UTF_8); } return value; }
private Encryptor(final String secretKey) { String actualKey = secretKey; if (actualKey.length() < 16) { StringBuilder actualKeyPadding = new StringBuilder(actualKey); int length = 16 - actualKey.length(); String randomChars = SecureRandomUtils.generateRandomPassword(length); actualKeyPadding.append(randomChars); actualKey = actualKeyPadding.toString(); LOG.warn("The secret key is too short (< 16), adding some random characters. " + "Passwords encrypted with AES and this key will not be recoverable " + "as a result if the container is restarted."); } try { keySpec = new SecretKeySpec(ArrayUtils.subarray( actualKey.getBytes(StandardCharsets.UTF_8), 0, 16), CipherAlgorithm.AES.getAlgorithm()); } catch (Exception e) { LOG.error("Error during key specification", e); } }
private Encryptor(final String secretKey) { String actualKey = secretKey; if (actualKey.length() < 16) { StringBuilder actualKeyPadding = new StringBuilder(actualKey); int length = 16 - actualKey.length(); String randomChars = SecureRandomUtils.generateRandomPassword(length); actualKeyPadding.append(randomChars); actualKey = actualKeyPadding.toString(); LOG.warn("The secret key is too short (< 16), adding some random characters. " + "Passwords encrypted with AES and this key will not be recoverable " + "as a result if the container is restarted."); } try { keySpec = new SecretKeySpec(ArrayUtils.subarray( actualKey.getBytes(StandardCharsets.UTF_8), 0, 16), CipherAlgorithm.AES.getAlgorithm()); } catch (Exception e) { LOG.error("Error during key specification", e); } }
public String encode(final String value, final CipherAlgorithm cipherAlgorithm) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { String encodedValue = null; if (value != null) { if (cipherAlgorithm == null || cipherAlgorithm == CipherAlgorithm.AES) { final byte[] cleartext = value.getBytes(StandardCharsets.UTF_8); final Cipher cipher = Cipher.getInstance(CipherAlgorithm.AES.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, keySpec); encodedValue = new String(Base64.getEncoder().encode(cipher.doFinal(cleartext))); } else if (cipherAlgorithm == CipherAlgorithm.BCRYPT) { encodedValue = BCrypt.hashpw(value, BCrypt.gensalt()); } else { encodedValue = getDigester(cipherAlgorithm).digest(value); } } return encodedValue; }
public String encode(final String value, final CipherAlgorithm cipherAlgorithm) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException { String encodedValue = null; if (value != null) { if (cipherAlgorithm == null || cipherAlgorithm == CipherAlgorithm.AES) { final byte[] cleartext = value.getBytes(StandardCharsets.UTF_8); final Cipher cipher = Cipher.getInstance(CipherAlgorithm.AES.getAlgorithm()); cipher.init(Cipher.ENCRYPT_MODE, keySpec); encodedValue = new String(Base64.getEncoder().encode(cipher.doFinal(cleartext))); } else if (cipherAlgorithm == CipherAlgorithm.BCRYPT) { encodedValue = BCrypt.hashpw(value, BCrypt.gensalt()); } else { encodedValue = getDigester(cipherAlgorithm).digest(value); } } return encodedValue; }
private StandardStringDigester getDigester(final CipherAlgorithm cipherAlgorithm) { StandardStringDigester digester = digesters.get(cipherAlgorithm); if (digester == null) { digester = new StandardStringDigester(); if (cipherAlgorithm.getAlgorithm().startsWith("S-")) { // Salted ... digester.setAlgorithm(cipherAlgorithm.getAlgorithm().replaceFirst("S\\-", "")); digester.setIterations(SALT_ITERATIONS); digester.setSaltSizeBytes(SALT_SIZE_BYTES); digester.setInvertPositionOfPlainSaltInEncryptionResults(IPOPSIER); digester.setInvertPositionOfSaltInMessageBeforeDigesting(IPOSIMBD); digester.setUseLenientSaltSizeCheck(ULSSC); } else { // Not salted ... digester.setAlgorithm(cipherAlgorithm.getAlgorithm()); digester.setIterations(1); digester.setSaltSizeBytes(0); } digester.setStringOutputType(CommonUtils.STRING_OUTPUT_TYPE_HEXADECIMAL); digesters.put(cipherAlgorithm, digester); } return digester; } }
private StandardStringDigester getDigester(final CipherAlgorithm cipherAlgorithm) { StandardStringDigester digester = new StandardStringDigester(); if (cipherAlgorithm.getAlgorithm().startsWith("S-")) { // Salted ... digester.setAlgorithm(cipherAlgorithm.getAlgorithm().replaceFirst("S\\-", "")); digester.setIterations(SALT_ITERATIONS); digester.setSaltSizeBytes(SALT_SIZE_BYTES); digester.setInvertPositionOfPlainSaltInEncryptionResults(IPOPSIER); digester.setInvertPositionOfSaltInMessageBeforeDigesting(IPOSIMBD); digester.setUseLenientSaltSizeCheck(ULSSC); } else { // Not salted ... digester.setAlgorithm(cipherAlgorithm.getAlgorithm()); digester.setIterations(1); digester.setSaltSizeBytes(0); } digester.setStringOutputType(CommonUtils.STRING_OUTPUT_TYPE_HEXADECIMAL); return digester; } }