String encrypt64()

in microprofile-extensions/microprofile-extensions-config/secured-string-converter/src/main/java/org/apache/geronimo/microprofile/extensions/config/converter/secure/PBECipher.java [79:102]


    String encrypt64(final String clearText, final byte[] password) {
        try {
            final byte[] clearBytes = clearText.getBytes(StandardCharsets.UTF_8);
            final byte[] salt = getSalt(SALT_SIZE);
            if (secureRandom != null) {
                new SecureRandom().nextBytes(salt);
            }

            final Cipher cipher = createCipher(password, salt, Cipher.ENCRYPT_MODE);
            final byte[] encryptedBytes = cipher.doFinal(clearBytes);
            final int len = encryptedBytes.length;
            final byte padLen = (byte) (CHUNK_SIZE - (SALT_SIZE + len + 1) % CHUNK_SIZE);
            final int totalLen = SALT_SIZE + len + padLen + 1;
            final byte[] allEncryptedBytes = getSalt(totalLen);
            System.arraycopy(salt, 0, allEncryptedBytes, 0, SALT_SIZE);
            allEncryptedBytes[SALT_SIZE] = padLen;
            System.arraycopy(encryptedBytes, 0, allEncryptedBytes, SALT_SIZE + 1, len);

            final byte[] encryptedTextBytes = Base64.getEncoder().encode(allEncryptedBytes);
            return new String(encryptedTextBytes, StandardCharsets.UTF_8);
        } catch (Exception e) {
            throw new IllegalArgumentException(e);
        }
    }