public static String encrypt(String secretKey, String plainText) {
        if (plainText == null) {
            return null;
        } else if (plainText.startsWith("$CRYPT::")) {
             // Avoid encrypting already encrypted text - APIMAN-1201
             return plainText;
        }
        
        byte[] encrypted;
        Cipher cipher;
        try {
            SecretKeySpec skeySpec = keySpecFromSecretKey(secretKey);
            
            cipher = Cipher.getInstance("AES"); //$NON-NLS-1$
            cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
            throw new RuntimeException(e);
        }
        try {
            encrypted = cipher.doFinal(plainText.getBytes());
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            throw new RuntimeException(e);
        }
        return "$CRYPT::" + new String(Base64.encodeBase64(encrypted)); //$NON-NLS-1$
    }
public static String encrypt ( String secretKey , String plainText ) { if ( plainText == null ) { return null ; } else if ( plainText . startsWith ( "$CRYPT::" ) ) { return plainText ; } byte [ ] encrypted ; Cipher cipher ; try { SecretKeySpec skeySpec = keySpecFromSecretKey ( secretKey ) ; cipher = Cipher . getInstance ( "AES" ) ; cipher . init ( Cipher . ENCRYPT_MODE , skeySpec ) ; } catch ( NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e ) { throw new RuntimeException ( e ) ; } try { encrypted = cipher . doFinal ( plainText . getBytes ( ) ) ; } catch ( IllegalBlockSizeException | BadPaddingException e ) { throw new RuntimeException ( e ) ; } return "$CRYPT::" + new String ( Base64 . encodeBase64 ( encrypted ) ) ; }
Encrypt.
@param plainText the plain text
@param secretKey the secret key
@return the string