yoyo.framework.standard.shared.SecurityUtils.java Source code

Java tutorial

Introduction

Here is the source code for yoyo.framework.standard.shared.SecurityUtils.java

Source

// ========================================================================
// Copyright (C) YOYO Project Team. All rights reserved.
// GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
// http://www.gnu.org/licenses/agpl-3.0.txt
// ========================================================================
package yoyo.framework.standard.shared;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

/**
 * 
 * <dl>
 * <dt>??</dt>
 * <dd>AES256bit?????JCE?????
 * %JAVA_HOME%/jre/lib/security?US_export_policy.jar?local_policy.jar?????</dd>
 * </dl>
 * @author nilcy
 */
public class SecurityUtils {
    /** ???? */
    private static final String ALGO_NAME = "AES";
    /** ??? */
    private static final String RAND_NAME = "SHA1PRNG";
    /** ?(128,JCE???256) TODO 256bit? */
    private static final int KEY_LENGTH = 128;

    /** ? */
    private SecurityUtils() {
    }

    /**
     * ???
     * <ul>
     * <li>AES256bit?????</li>
     * <li>SHA1PRNG???????URL?BASE64?????</li>
     * </ul>
     * @return ?(URL?BASE64)
     */
    public static String createKey() {
        try {
            final KeyGenerator generator = KeyGenerator.getInstance(ALGO_NAME);
            generator.init(KEY_LENGTH, SecureRandom.getInstance(RAND_NAME));
            return Base64.encodeBase64URLSafeString(generator.generateKey().getEncoded());
        } catch (final NoSuchAlgorithmException e) {
            throw new IllegalArgumentException(e);
        }
    }

    /**
     * ?
     * @param plainBytes ?
     * @param base64Key ?
     * @return ??
     */
    public static byte[] encrypt(final byte[] plainBytes, final String base64Key) {
        return exec(plainBytes, base64Key, Cipher.ENCRYPT_MODE);
    }

    /**
     * ?
     * @param encryptedBytes ??
     * @param base64Key ?
     * @return ?
     */
    public static byte[] decrypt(final byte[] encryptedBytes, final String base64Key) {
        return exec(encryptedBytes, base64Key, Cipher.DECRYPT_MODE);
    }

    /**
     * ?/?
     * @param encryptedBytes ?(/?)
     * @param base64Key ?
     * @param cipherMode ?/?
     * @return ?(?/)
     */
    private static byte[] exec(final byte[] encryptedBytes, final String base64Key, final int cipherMode) {
        try {
            final Cipher cipher = Cipher.getInstance(ALGO_NAME);
            cipher.init(cipherMode, new SecretKeySpec(Base64.decodeBase64(base64Key), ALGO_NAME));
            return cipher.doFinal(encryptedBytes);
        } catch (final InvalidKeyException e) {
            e.printStackTrace();
            throw new StandardRuntimeException(e);
        } catch (final NoSuchAlgorithmException e) {
            throw new StandardRuntimeException(e);
        } catch (final NoSuchPaddingException e) {
            throw new StandardRuntimeException(e);
        } catch (final IllegalBlockSizeException e) {
            throw new StandardRuntimeException(e);
        } catch (final BadPaddingException e) {
            throw new StandardRuntimeException(e);
        }
    }
}