interconnect/model/src/main/java/de/taimos/dvalin/interconnect/model/MessageCryptoUtil.java
/**
     * @param data the data to encrypt
     * @return the encrypted BASE64 data
     * @throws CryptoException on encryption error
     */
@param data the data to encrypt
@return the encrypted BASE64 data
@throws CryptoException on encryption error
@param data the data to encrypt @return the encrypted BASE64 data @throws CryptoException on encryption error
public static String crypt(final String data) throws CryptoException {
        if (data == null) {
            return null;
        }
        try {
            SecureRandom secureRandom = new SecureRandom();
            byte[] iv = new byte[12]; 
            secureRandom.nextBytes(iv);
            GCMParameterSpec parameterSpec = new GCMParameterSpec(128, iv);
            final Cipher cipher = MessageCryptoUtil.getCipher(Cipher.ENCRYPT_MODE, parameterSpec);
            final byte[] encrypted = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
            ByteBuffer byteBuffer = ByteBuffer.allocate(iv.length + encrypted.length);
            byteBuffer.put(iv);
            byteBuffer.put(encrypted);
            byte[] cipherMessage = byteBuffer.array();
            return Base64.encodeBase64String(cipherMessage);
        } catch (final Exception e) {
            throw new CryptoException("Encryption of data failed!", e);
        }
    }
public static String crypt ( final String data ) throws CryptoException { if ( data == null ) { return null ; } try { SecureRandom secureRandom = new SecureRandom ( ) ; byte [ ] iv = new byte [ 12 ] ; secureRandom . nextBytes ( iv ) ; GCMParameterSpec parameterSpec = new GCMParameterSpec ( 128 , iv ) ; final Cipher cipher = MessageCryptoUtil . getCipher ( Cipher . ENCRYPT_MODE , parameterSpec ) ; final byte [ ] encrypted = cipher . doFinal ( data . getBytes ( StandardCharsets . UTF_8 ) ) ; ByteBuffer byteBuffer = ByteBuffer . allocate ( iv . length + encrypted . length ) ; byteBuffer . put ( iv ) ; byteBuffer . put ( encrypted ) ; byte [ ] cipherMessage = byteBuffer . array ( ) ; return Base64 . encodeBase64String ( cipherMessage ) ; } catch ( final Exception e ) { throw new CryptoException ( "Encryption of data failed!" , e ) ; } }
@param data the data to encrypt
 @return the encrypted BASE64 data
 @throws CryptoException on encryption error
@param data the data to encrypt @return the encrypted BASE64 data @throws CryptoException on encryption error
//NEVER REUSE THIS IV WITH SAME KEY