public String encrypt(String data) {

        Objects.requireNonNull(data, "Data to be encrypted");
        
        if (!secretKeySpec.isPresent()) {
            return data;
        }

        try {
            // encrypt data
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec.get());
            byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));

            // convert encrypted bytes to string in base64
            return Base64.encodeBase64URLSafeString(encrypted);

        } catch (InvalidKeyException ex) {
            logger.error(getHelperLogMessage(), ex);
            throw new RuntimeException(ex);
        } catch (GeneralSecurityException ex) {
            logger.error("Failed to encrypt data.", ex);
            return "";
        }
    }
public String encrypt(String data) {

        Objects.requireNonNull(data, "Data to be encrypted");
        
        if (!secretKeySpec.isPresent()) {
            return data;
        }

        try {
            
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec.get());
            byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));

            
            return Base64.encodeBase64URLSafeString(encrypted);

        } catch (InvalidKeyException ex) {
            logger.error(getHelperLogMessage(), ex);
            throw new RuntimeException(ex);
        } catch (GeneralSecurityException ex) {
            logger.error("Failed to encrypt data.", ex);
            return "";
        }
    }
public String encrypt ( String data ) { Objects . requireNonNull ( data , "Data to be encrypted" ) ; if ( ! secretKeySpec . isPresent ( ) ) { return data ; } try { // encrypt data Cipher cipher = Cipher . getInstance ( ALGORITHM ) ; cipher . init ( Cipher . ENCRYPT_MODE , secretKeySpec . get ( ) ) ; byte [ ] encrypted = cipher . doFinal ( data . getBytes ( StandardCharsets . UTF_8 ) ) ; // convert encrypted bytes to string in base64 return Base64 . encodeBase64URLSafeString ( encrypted ) ; } catch ( InvalidKeyException ex ) { logger . error ( getHelperLogMessage ( ) , ex ) ; throw new RuntimeException ( ex ) ; } catch ( GeneralSecurityException ex ) { logger . error ( "Failed to encrypt data." , ex ) ; return "" ; } }
Encrypts data with secret key.

@param data text to encrypt
@return encrypted text in base64 format
Encrypts data with secret key .