All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.svenkubiak.ninja.auth.utils.CryptoUtils Maven / Gradle / Ivy

package de.svenkubiak.ninja.auth.utils;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;

import ninja.utils.NinjaProperties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Preconditions;
import com.google.inject.Inject;
import com.google.inject.Singleton;

import de.svenkubiak.ninja.auth.enums.Constants;

/**
 * 
 * @author svenkubiak
 *
 */
@Singleton
public class CryptoUtils {
    private static final Logger LOG = LoggerFactory.getLogger(CryptoUtils.class);
    private static final String ENCODING = "UTF-8";
    private static final String ENCRYPTION = "AES";
    private static final int START = 0;
    private static final int END = 16;
    private SecretKeySpec secretKeySpec;
    private Cipher cipher;
    private NinjaProperties ninjaProperties;
    
    @Inject
    public CryptoUtils(NinjaProperties ninjaProperties) {
        this.ninjaProperties = ninjaProperties;
        
        try {
            this.cipher = Cipher.getInstance(ENCRYPTION);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            LOG.error("Failed to create AES cipher", e);
        }
        
        try {
            byte[] key = (this.ninjaProperties.get(Constants.APPLICATION_SECRET.get()).substring(START, END)).getBytes(ENCODING);
            this.secretKeySpec = new SecretKeySpec(key, ENCRYPTION);  
        } catch (UnsupportedEncodingException e) {
            LOG.error("Failed to get secret key", e);
        }
    }
    
    /**
     * Encrypt a plain text using AES
     * 
     * @param plainText The plain text to encrypt
     * 
     * @return The encrypted text or null if encryption fails
     */
    public String encrypt(String plainText) {
        Preconditions.checkNotNull(plainText, "plainText is required for encryption");
        
        String encrypted = null;
        try {
            cipher.init(Cipher.ENCRYPT_MODE, this.secretKeySpec);
            encrypted = Base64.getEncoder().encodeToString(cipher.doFinal(plainText.getBytes(ENCODING)));
        } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e) {
            LOG.error("Failed to encrypt plain text", e);
        }
         
        return encrypted;
    }

    /**
     * Decrypt a plain text using AES
     * 
     * @param encryptedText The encrypted text
     * 
     * @return The plain text or null if decryptions fails
     */
    public String decrypt(String encryptedText) {
        Preconditions.checkNotNull(encryptedText, "encryptedText is required for encryption");
        
        String decrypted = null;
        try {
            cipher.init(Cipher.DECRYPT_MODE, this.secretKeySpec);
            decrypted = new String(cipher.doFinal(Base64.getDecoder().decode(encryptedText)), ENCODING);
        } catch (InvalidKeyException | IllegalBlockSizeException | BadPaddingException | UnsupportedEncodingException e) {
            LOG.error("Failed to decrypt plain text", e);
        }
        
        return decrypted;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy