jp.co.golorp.emarf.util.CryptUtil.java Source code

Java tutorial

Introduction

Here is the source code for jp.co.golorp.emarf.util.CryptUtil.java

Source

/*
 * Apache License, Version 2.0???????
 * ?????????????
 *
 * ??http://www.apache.org/licenses/LICENSE-2.0?????
 *
 * ???????????????
 * ????????
 * ??????????????????????
 *
 * ????????????????????????
 */
package jp.co.golorp.emarf.util;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ResourceBundle;

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

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jp.co.golorp.emarf.exception.SystemError;

/**
 * ?
 *
 * @author oukuf@golorp
 */
public final class CryptUtil {

    /***/
    private static final Logger LOG = LoggerFactory.getLogger(CryptUtil.class);

    /** CryptUtil.properties */
    private static final ResourceBundle BUNDLE = ResourceBundle.getBundle(CryptUtil.class.getSimpleName());

    /** ?16 */
    private static final byte[] SECRET_KEY = BUNDLE.getString("secret_key").getBytes(); //$NON-NLS-1$

    /** ?? */
    private static final String ALGORITHM = BUNDLE.getString("algorithm"); //$NON-NLS-1$

    /**
     * 
     */
    private CryptUtil() {
    }

    /**
     * 16???AES???Base64????
     *
     * @param string
     *            
     * @return ?
     */
    public static String encrypt(final String string) {

        if (string == null) {
            return null;
        }

        byte[] input = string.getBytes();

        byte[] encryped = cipher(Cipher.ENCRYPT_MODE, input);

        byte[] encoded = Base64.encodeBase64(encryped, false);

        String ret = new String(encoded);

        LOG.debug("Encrypt [" + string + "] to [" + ret + "].");

        return ret;
    }

    /**
     * Base64??AES????
     *
     * @param encryped
     *            ?
     * @return ?
     */
    public static String decrypt(final String encryped) {

        if (encryped == null) {
            return null;
        }

        byte[] input = Base64.decodeBase64(encryped);

        byte[] decrypted = cipher(Cipher.DECRYPT_MODE, input);

        String ret = new String(decrypted);

        LOG.debug("Decrypt [" + encryped + "] to [" + ret + "].");

        return ret;
    }

    /**
     * ???
     *
     * @param opmode
     *            opmode
     * @param input
     *            input
     * @return byte[]
     */
    private static byte[] cipher(final int opmode, final byte[] input) {

        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance(ALGORITHM);
        } catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
            throw new SystemError(e);
        }

        try {
            cipher.init(opmode, new SecretKeySpec(SECRET_KEY, ALGORITHM));
        } catch (InvalidKeyException e) {
            throw new SystemError(e);
        }

        byte[] bytes = null;
        try {
            LOG.trace(String.valueOf(input));
            bytes = cipher.doFinal(input);
        } catch (IllegalBlockSizeException | BadPaddingException e) {
            throw new SystemError(e);
        }

        return bytes;
    }

}