org.javaweb.utils.EncryptUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.javaweb.utils.EncryptUtils.java

Source

/*
 * Copyright yz 2016-01-14  Email:admin@javaweb.org.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.javaweb.utils;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.security.NoSuchAlgorithmException;

/**
 * ???
 *
 * @author yz
 */
public class EncryptUtils {

    /**
     * MD5
     *
     * @param context
     * @return
     */
    public static String md5(String context) {
        return DigestUtils.md5Hex(context);
    }

    /**
     * MD5
     *
     * @param bytes
     * @return
     */
    public static String md5(byte[] bytes) {
        return DigestUtils.md5Hex(bytes);
    }

    /**
     * MD5
     *
     * @param in
     * @return
     * @throws IOException
     */
    public static String md5(InputStream in) throws IOException {
        return DigestUtils.md5Hex(in);
    }

    /**
     * Base64?
     *
     * @param str
     * @return
     */
    public static String base64Encode(String str) {
        return new String(base64Encode(str.getBytes()));
    }

    /**
     * Base64?
     *
     * @param bytes
     * @return
     */
    public static byte[] base64Encode(byte[] bytes) {
        return Base64.encodeBase64(bytes);
    }

    /**
     * Base64?
     *
     * @param str
     * @return
     */
    public static String base64Decode(String str) {
        return new String(base64Decode(str.getBytes()));
    }

    /**
     * Base64?
     *
     * @param bytes
     * @return
     */
    public static byte[] base64Decode(byte[] bytes) {
        return Base64.decodeBase64(bytes);
    }

    /**
     * ?AES 
     *
     * @param length
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String getAESKey(int length) throws NoSuchAlgorithmException {
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(length);
        SecretKey secretKey = keyGenerator.generateKey();

        return HexUtils.bytes2HexString(secretKey.getEncoded());
    }

    /**
     * AES ?
     *
     * @param content
     * @param key
     * @return
     * @throws java.lang.Exception
     */
    public static String encryptionAES(String content, String key) throws Exception {
        SecretKeySpec secretKeySpec = new SecretKeySpec(HexUtils.hex2Bytes(key), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec);

        return HexUtils.bytes2HexString(cipher.doFinal(content.getBytes()));
    }

    /**
     * AES ?
     *
     * @param content
     * @param key
     * @return
     * @throws java.lang.Exception
     */
    public static String decryptionAES(String content, String key) throws Exception {
        SecretKeySpec sKeySpec = new SecretKeySpec(HexUtils.hex2Bytes(key), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, sKeySpec);
        byte[] bytes = cipher.doFinal(HexUtils.hex2Bytes(content));

        return new String(bytes);
    }

    /**
     * RC4
     *
     * @param data ?
     * @param key  key
     * @return
     */
    public static String decryptionRC4(byte[] data, String key) {
        if (data == null || key == null) {
            return null;
        }

        return new String(RC4EncryptUtils.rc4(data, key));
    }

    /**
     * RC4
     *
     * @param data ?
     * @param key  key
     * @return
     */
    public static byte[] encryptionRC4Byte(String data, String key) {
        try {
            return encryptionRC4Byte(data.getBytes(StandardCharsets.UTF_8), key);
        } catch (NullPointerException e) {
            return null;
        }
    }

    /**
     * RC4
     *
     * @param bytes ?
     * @param key   key
     * @return
     */
    public static byte[] encryptionRC4Byte(byte[] bytes, String key) {
        if (bytes == null || key == null) {
            return null;
        }

        return RC4EncryptUtils.rc4(bytes, key);
    }

}