Java加密工具類

1、添加依賴java

<dependency>
    <groupId>org.bouncycastle</groupId>
    <artifactId>bcprov-jdk16</artifactId>
    <version>1.46</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.9</version>
</dependency>
<dependency>
    <groupId>commons-lang</groupId>
    <artifactId>commons-lang</artifactId>
    <version>2.6</version>
</dependency>

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.6</version>
</dependency>

2、加密工具類算法

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;

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

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

/**
 * @author rico
 * md5加密工具
 * message-digest algorithm 5 (信息-摘要算法),經常使用於文件校驗
 *
 * SHA(Secure Hash Algorithm,安全散列算法)
 * 數字簽名等密碼學應用中重要的工具,被普遍地應用於電子商務等信息安全領域。
 * SHA與MD5經過碰撞法都被破解了,可是SHA仍然是公認的安全加密算法,較之MD5更爲安全
 * https://my.oschina.net/u/733161/blog/756814
 */
public class EncryptUtil {
    private static final String CHARSET_UTF8 = "UTF-8";

    static {
        //TODO add Provider
        // Adds a provider to the next position available.
        Security.addProvider(new BouncyCastleProvider());
    }


    //============================================================================
    // 單向加密 MD5,SHA
    //============================================================================
    /**
     * 單向加密
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static byte[] digest(String algorithm, String value) throws NoSuchAlgorithmException {
        if(StringUtils.isBlank(algorithm)) {
            throw new NullPointerException("algorithm is null.");
        }
        if(StringUtils.isBlank(value)) {
            throw new NullPointerException("digest value is null.");
        }

        MessageDigest digest = MessageDigest.getInstance(algorithm);
        digest.update(value.getBytes());

        return digest.digest();
    }

    /**
     * 單向加密並轉換爲字符串格式
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String digestToString(String algorithm, String value) throws NoSuchAlgorithmException {
        byte[] bytes = digest(algorithm, value);
        return Base64.encodeBase64String(bytes);
    }

    /**
     * md5 加密
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static byte[] md5(String value) throws NoSuchAlgorithmException {
        return digest(EncryptAlgorithmEnum.MD5.value(), value);
    }

    /**
     * md5加密並轉換爲字符串格式
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String md5ToString(String value) throws NoSuchAlgorithmException {
        return digestToString(EncryptAlgorithmEnum.MD5.value(), value);
    }

    /**
     * sha加密
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static byte[] sha(String value) throws NoSuchAlgorithmException {
        return digest(EncryptAlgorithmEnum.SHA.value(), value);
    }

    /**
     * sha加密並轉換爲字符串格式
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String shaToString(String value) throws NoSuchAlgorithmException {
        return digestToString(EncryptAlgorithmEnum.SHA.value(), value);
    }


    //===============================================================================
    // 對稱雙向加密AES,DES
    //===============================================================================
    /**
     * 生成密鑰
     * @param algorithm
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static SecretKey generateSecretKey(String algorithm) throws NoSuchAlgorithmException {
        if(StringUtils.isBlank(algorithm)) {
            throw new NullPointerException("algorithm name is null");
        }
        KeyGenerator keyGenerator = KeyGenerator.getInstance(algorithm);
        return keyGenerator.generateKey();
    }

    /**
     * 生成密鑰字符串
     * @param algorithm
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String generateSecretKeyString(String algorithm) throws NoSuchAlgorithmException {
        SecretKey secretKey = generateSecretKey(algorithm);
        return Base64.encodeBase64String(secretKey.getEncoded());
    }

    /**
     * 生成AES密鑰
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static SecretKey aesKey() throws NoSuchAlgorithmException {
        return generateSecretKey(EncryptAlgorithmEnum.AES.value());
    }

    /**
     * 生成AES密鑰字符串
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String aesKeyString() throws NoSuchAlgorithmException {
        return generateSecretKeyString(EncryptAlgorithmEnum.AES.value());
    }

    /**
     * 生成DES密鑰
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static SecretKey desKey() throws NoSuchAlgorithmException {
        return generateSecretKey(EncryptAlgorithmEnum.DES.value());
    }

    /**
     * 生成DES密鑰字符串
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static String desKeyString() throws NoSuchAlgorithmException {
        return generateSecretKeyString(EncryptAlgorithmEnum.DES.value());
    }

    /**
     * 解析SecretKey
     * @param algorithm
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static SecretKey parseSecretKey(String algorithm, String value) throws NoSuchAlgorithmException {
        byte[] bytes = Base64.decodeBase64(value);
        return parseSecretKey(algorithm, bytes);
    }

    /**
     * 解析SecretKey
     * @param algorithm
     * @param bytes
     * @return
     */
    public static SecretKey parseSecretKey(String algorithm, byte[] bytes) {
        SecretKey secretKey = new SecretKeySpec(bytes, 0, bytes.length, algorithm);
        return secretKey;
    }

    /**
     * 解析 AES SecretKey
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static SecretKey parseAESSecretKey(String value) throws NoSuchAlgorithmException {
        return parseSecretKey(EncryptAlgorithmEnum.AES.value(), value);
    }
    public static SecretKey parseAESSecretKey(byte[] byets) throws NoSuchAlgorithmException {
        return parseSecretKey(EncryptAlgorithmEnum.AES.value(), byets);
    }

    /**
     * 解析 DES SecretKey
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static SecretKey parseDESSecretKey(String value) throws NoSuchAlgorithmException {
        return parseSecretKey(EncryptAlgorithmEnum.DES.value(), value);
    }
    public static SecretKey parseDESSecretKey(byte[] bytes) throws NoSuchAlgorithmException {
        return parseSecretKey(EncryptAlgorithmEnum.DES.value(), bytes);
    }

    /**
     * 對稱加密
     * @param algorithm
     * @param key
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static byte[] encryptSymmetric(String algorithm, SecretKey key, String value) throws NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        if(StringUtils.isBlank(algorithm)) {
            throw new NullPointerException("algorithm name is null");
        }
        if(StringUtils.isBlank(value)) {
            throw new NullPointerException("encrypt value is null");
        }

        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(value.getBytes());
    }
    public static String encryptSymmetricString(String algorithm, SecretKey key, String value) throws NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        byte[] bytes = encryptSymmetric(algorithm, key, value);
        return Base64.encodeBase64String(bytes);
    }

    /**
     * 對稱解密
     * @param algorithm
     * @param key
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws UnsupportedEncodingException
     */
    public static byte[] decryptSymmetric(String algorithm, SecretKey key, String value) throws NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {

        if(StringUtils.isBlank(algorithm)) {
            throw new NullPointerException("algorithm name is null");
        }
        if(StringUtils.isBlank(value)) {
            throw new NullPointerException("decrypt value is null");
        }

        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, key);

        byte[] decodeBytes = Base64.decodeBase64(value);
        return cipher.doFinal(decodeBytes);
    }
    public static String decryptSymmetricString(String algorithm, SecretKey key, String value) throws NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        byte[] bytes = decryptSymmetric(algorithm, key, value);
        return new String(bytes, CHARSET_UTF8);
    }

    /**
     * DES對稱加密
     * @param key
     * @param value
     * @return
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static byte[] desEncrypt(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        return encryptSymmetric(EncryptAlgorithmEnum.DES.value(), key, value);
    }
    public static String desEncryptString(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        return encryptSymmetricString(EncryptAlgorithmEnum.DES.value(), key, value);
    }

    /**
     * DES對稱解密
     * @param key
     * @param value
     * @return
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws UnsupportedEncodingException
     */
    public static String desDecryptString(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return decryptSymmetricString(EncryptAlgorithmEnum.DES.value(), key, value);
    }
    public static byte[] desDecrypt(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return decryptSymmetric(EncryptAlgorithmEnum.DES.value(), key, value);
    }

    /**
     * AES對稱加密
     * @param key
     * @param value
     * @return
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     */
    public static byte[] aesEncrypt(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        return encryptSymmetric(EncryptAlgorithmEnum.AES.value(), key, value);
    }
    public static String aesEncryptString(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException {
        return encryptSymmetricString(EncryptAlgorithmEnum.AES.value(), key, value);
    }

    /**
     * AES對稱解密
     * @param key
     * @param value
     * @return
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws UnsupportedEncodingException
     */
    public static byte[] aesDecrypt(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return decryptSymmetric(EncryptAlgorithmEnum.AES.value(), key, value);
    }
    public static String aesDecryptString(SecretKey key, String value) throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return decryptSymmetricString(EncryptAlgorithmEnum.AES.value(), key, value);
    }


    //=================================================================================
    // 非對稱雙向加密RSA/DSA
    //===========================非對稱雙向加密=========================================
    /**
     * 生成密鑰對
     * @param algorithm
     * @param keysize
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static KeyPair generateKeyPair(String algorithm, int keysize) throws NoSuchAlgorithmException {
        if(StringUtils.isBlank(algorithm)) {
            throw new NullPointerException("algorithm name is null");
        }
        if(keysize <= 0) {
            throw new IllegalArgumentException("keysize should be great than zero.");
        }

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
        keyPairGenerator.initialize(keysize);

        return keyPairGenerator.generateKeyPair();
    }

    /**
     * 生成密鑰對
     * @param algorithm
     * @param keysize
     * @param random
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static KeyPair generateKeyPair(String algorithm, int keysize, SecureRandom random) throws NoSuchAlgorithmException {
        if(StringUtils.isBlank(algorithm)) {
            throw new NullPointerException("algorithm name is null");
        }
        if(keysize <= 0) {
            throw new IllegalArgumentException("keysize should be great than zero.");
        }

        KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance(algorithm);
        keyPairGenerator.initialize(keysize, random);

        return keyPairGenerator.generateKeyPair();
    }

    /**
     * 獲取RSA密鑰對
     * @param keysize
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static KeyPair rsaKeyPair(int keysize) throws NoSuchAlgorithmException {
        return generateKeyPair(EncryptAlgorithmEnum.RSA.value(), keysize);
    }
    public static KeyPair rsaKeyPair(int keysize, SecureRandom random) throws NoSuchAlgorithmException {
        return generateKeyPair(EncryptAlgorithmEnum.RSA.value(), keysize, random);
    }

    /**
     * 獲取DSA密鑰對
     * @param keysize
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static KeyPair dsaKeyPair(int keysize) throws NoSuchAlgorithmException {
        return generateKeyPair(EncryptAlgorithmEnum.DSA.value(), keysize);
    }
    public static KeyPair dsaKeyPair(int keysize, SecureRandom random) throws NoSuchAlgorithmException {
        return generateKeyPair(EncryptAlgorithmEnum.DSA.value(), keysize, random);
    }

    public static String keyToString(Key key) {
        return Base64.encodeBase64String(key.getEncoded());
    }

    /**
     * 非對稱加密
     * @param algorithm
     * @param key
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws UnsupportedEncodingException
     */
    public static byte[] encryptNonSymmetric(String algorithm, Key key, String value) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        if(StringUtils.isBlank(algorithm)) {
            throw new NullPointerException("algorithm name is null");
        }
        if(StringUtils.isBlank(value)) {
            throw new NullPointerException("encrypt value is null");
        }

        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(value.getBytes(CHARSET_UTF8));
    }
    public static String encryptNonSymmetricString(String algorithm, Key key, String value) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        byte[] bytes = encryptNonSymmetric(algorithm, key, value);
        return Base64.encodeBase64String(bytes);
    }

    /**
     * 非對稱解密
     * @param algorithm
     * @param key
     * @param value
     * @return
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws UnsupportedEncodingException
     */
    public static byte[] decryptNonSymmetric(String algorithm, Key key, String value) throws NoSuchAlgorithmException, NoSuchPaddingException,
            InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        if(StringUtils.isBlank(algorithm)) {
            throw new NullPointerException("algorithm name is null");
        }
        if(StringUtils.isBlank(value)) {
            throw new NullPointerException("decrypt value is null");
        }

        byte[] bytes = Base64.decodeBase64(value);
        Cipher cipher = Cipher.getInstance(algorithm);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(bytes);
    }
    public static String decryptNonSymmetricString(String algorithm, Key key, String value) throws NoSuchAlgorithmException,
            NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        byte[] decryptBytes = decryptNonSymmetric(algorithm, key, value);
        return new String(decryptBytes, CHARSET_UTF8);
    }

    /**
     * RSA加密
     * @param key
     * @param value
     * @return
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws UnsupportedEncodingException
     */
    public static byte[] rsaEncrypt(Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
            IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return encryptNonSymmetric(EncryptAlgorithmEnum.RSA.value(), key, value);
    }
    public static String rsaEncryptString(Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
            IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return encryptNonSymmetricString(EncryptAlgorithmEnum.RSA.value(), key, value);
    }

    /**
     * RSA解密
     * @param key
     * @param value
     * @return
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws UnsupportedEncodingException
     */
    public static byte[] rsaDecrypt(Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
            IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return decryptNonSymmetric(EncryptAlgorithmEnum.RSA.value(), key, value);
    }
    public static String rsaDecryptString(Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
            IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return decryptNonSymmetricString(EncryptAlgorithmEnum.RSA.value(), key, value);
    }

    /**
     * DSA加密
     * @param key
     * @param value
     * @return
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws UnsupportedEncodingException
     */
    public static byte[] dsaEncrypt(Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
            IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return encryptNonSymmetric(EncryptAlgorithmEnum.DSA.value(), key, value);
    }
    public static String dsaEncryptString(Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException,
            IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return encryptNonSymmetricString(EncryptAlgorithmEnum.DSA.value(), key, value);
    }

    /**
     * DSA解密
     * @param algorithm
     * @param key
     * @param value
     * @return
     * @throws InvalidKeyException
     * @throws NoSuchAlgorithmException
     * @throws NoSuchPaddingException
     * @throws IllegalBlockSizeException
     * @throws BadPaddingException
     * @throws UnsupportedEncodingException
     */
    public static byte[] dsaDecrypt(String algorithm, Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return decryptNonSymmetric(EncryptAlgorithmEnum.DSA.value(), key, value);
    }
    public static String dsaDecryptString(String algorithm, Key key, String value) throws InvalidKeyException, NoSuchAlgorithmException,
            NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {
        return decryptNonSymmetricString(EncryptAlgorithmEnum.DSA.value(), key, value);
    }

    /**
     * 解析私鑰
     * @param keyFactory
     * @param bytes
     * @return
     * @throws InvalidKeySpecException
     */
    public static PrivateKey parsePrivateKey(KeyFactory keyFactory, byte[] bytes) throws InvalidKeySpecException {
        // Creates a new PKCS8EncodedKeySpec with the given encoded key.
        // PKCS8EncodedKeySpec represents the ASN.1 encoding of a private key, encoded according to the ASN.1 type PrivateKeyInfo.
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
        PrivateKey key = (PrivateKey) keyFactory.generatePrivate(keySpec);

        return key;
    }
    public static PrivateKey parsePrivateKey(KeyFactory keyFactory, String keyValue) throws InvalidKeySpecException {
        return parsePrivateKey(keyFactory, Base64.decodeBase64(keyValue.getBytes()));
    }

    /**
     * 解析私鑰(RSA)
     * @param bytes
     * @return
     * @throws InvalidKeySpecException
     * @throws NoSuchAlgorithmException
     */
    public static PrivateKey parseRSAPrivateKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
        KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.RSA.value());
        return parsePrivateKey(keyFactory, bytes);
    }
    public static PrivateKey parseRSAPrivateKey(String keyValue) throws InvalidKeySpecException, NoSuchAlgorithmException {
        KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.RSA.value());
        return parsePrivateKey(keyFactory, keyValue);
    }

    /**
     * 解析私鑰(DSA)
     * @param bytes
     * @return
     * @throws InvalidKeySpecException
     * @throws NoSuchAlgorithmException
     */
    public static PrivateKey parseDSAPrivateKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
        KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.DSA.value());
        return parsePrivateKey(keyFactory, bytes);
    }
    public static PrivateKey parseDSAPrivateKey(String keyValue) throws InvalidKeySpecException, NoSuchAlgorithmException {
        KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.DSA.value());
        return parsePrivateKey(keyFactory, keyValue);
    }

    /**
     * 解析公鑰
     * @param keyFactory
     * @param bytes
     * @return
     * @throws InvalidKeySpecException
     */
    public static PublicKey parsePublicKey(KeyFactory keyFactory, byte[] bytes) throws InvalidKeySpecException {
        // Creates a new X509EncodedKeySpec with the given encoded key.
        // X509EncodedKeySpec represents the ASN.1 encoding of a public key, encoded according to the ASN.1 type SubjectPublicKeyInfo.
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
        PublicKey key = keyFactory.generatePublic(keySpec);

        return key;
    }
    public static PublicKey parsePublicKey(KeyFactory keyFactory, String keyValue) throws InvalidKeySpecException {
        return parsePublicKey(keyFactory, Base64.decodeBase64(keyValue.getBytes()));
    }

    /**
     * 解析公鑰(RSA)
     * @param bytes
     * @return
     * @throws InvalidKeySpecException
     * @throws NoSuchAlgorithmException
     */
    public static PublicKey parseRSAPublicKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
        KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.RSA.value());
        return parsePublicKey(keyFactory, bytes);
    }
    public static PublicKey parseRSAPublicKey(String keyValue) throws InvalidKeySpecException, NoSuchAlgorithmException {
        KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.RSA.value());
        return parsePublicKey(keyFactory, keyValue);
    }

    /**
     * 解析公鑰(DSA)
     * @param bytes
     * @return
     * @throws InvalidKeySpecException
     * @throws NoSuchAlgorithmException
     */
    public static PublicKey parseDSAPublicKey(byte[] bytes) throws InvalidKeySpecException, NoSuchAlgorithmException {
        KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.DSA.value());
        return parsePublicKey(keyFactory, bytes);
    }
    public static PublicKey parseDSAPublicKey(String keyValue) throws InvalidKeySpecException, NoSuchAlgorithmException {
        KeyFactory keyFactory = getKeyFactory(EncryptAlgorithmEnum.DSA.value());
        return parsePublicKey(keyFactory, keyValue);
    }


    /**
     * 獲取祕鑰工廠類
     * @param algorithm
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static KeyFactory getKeyFactory(String algorithm) throws NoSuchAlgorithmException {
        return KeyFactory.getInstance(algorithm);
    }

    /**
     * 獲取祕鑰工廠類(RSA)
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static KeyFactory getRSAKeyFactory() throws NoSuchAlgorithmException {
        return getKeyFactory(EncryptAlgorithmEnum.RSA.value());
    }

    /**
     * 獲取祕鑰工廠類(DSA)
     * @return
     * @throws NoSuchAlgorithmException
     */
    public static KeyFactory getDSAKeyFactory() throws NoSuchAlgorithmException {
        return getKeyFactory(EncryptAlgorithmEnum.DSA.value());
    }

    public static enum EncryptAlgorithmEnum {
        DSA("dsa")
        ,RSA("rsa")
        ,AES("aes")
        ,DES("des")
        ,MD5("md5")
        ,SHA("sha");

        private String value;

        EncryptAlgorithmEnum(String value) {
            this.value = value;
        }

        public String value() {
            return value;
        }

    }

}
相關文章
相關標籤/搜索