对加密使用的整理:
1.需要导入jar包
'org.bouncycastle:bcprov-jdk16:1.46',
'commons-codec:commons-codec:1.9',
2.没有找到对应的加密算法名称,定义了算法名称Enum:EncryptAlgorithmEnum.java
/**
*
*/
package org.rico.commons.utils.encrypt;
/**
* @author rico
* 加密算法名称enum
*/
public enum EncryptAlgorithmEnum {
MD5("MD5"),
SHA("SHA"),
SHA1("SHA1"),
SHA256("SHA256"),
DSA("DSA"),
RSA("RSA"),
AES("AES"),
DES("DES"),
DESede("DESede");
private String name;
private EncryptAlgorithmEnum(String name) {
this.name = name;
}
public String value() {
return this.getName();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
3.加密工具类EncryptUtil.java
/**
*
*/
package org.rc.common.encrypt;
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 keyValue
* @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 keyValue
* @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 keyValue
* @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 keyValue
* @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 keyValue
* @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 keyValue
* @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)
* @param algorithm
* @return
* @throws NoSuchAlgorithmException
*/
public static KeyFactory getRSAKeyFactory() throws NoSuchAlgorithmException {
return getKeyFactory(EncryptAlgorithmEnum.RSA.value());
}
/**
* 获取秘钥工厂类(DSA)
* @param algorithm
* @return
* @throws NoSuchAlgorithmException
*/
public static KeyFactory getDSAKeyFactory() throws NoSuchAlgorithmException {
return getKeyFactory(EncryptAlgorithmEnum.DSA.value());
}
}
附: