KeyUtil中的读取KeyStore文件的方法增加全局Provider

This commit is contained in:
Looly 2022-12-27 19:52:33 +08:00
parent b78657f67f
commit 05c32a8f46
2 changed files with 30 additions and 14 deletions

View File

@ -11,6 +11,7 @@
* 【core 】 HtmlUtil中escape方法增加不断开空格nbsp转译防止xss攻击pr#2755@Github * 【core 】 HtmlUtil中escape方法增加不断开空格nbsp转译防止xss攻击pr#2755@Github
* 【extra 】 修正sftp.cd方法 方法注释和实际效果不符issue#2758@Github * 【extra 】 修正sftp.cd方法 方法注释和实际效果不符issue#2758@Github
* 【core 】 修改PhoneUtil容易歧义的注释issue#I63GWK@Gitee * 【core 】 修改PhoneUtil容易歧义的注释issue#I63GWK@Gitee
* 【crypto】 KeyUtil中的读取KeyStore文件的方法增加全局Providerissue#I6796G@Gitee
* *
### 🐞Bug修复 ### 🐞Bug修复
* 【json 】 修复普通byte数组转JSONArray时的异常pr#875@Gitee * 【json 】 修复普通byte数组转JSONArray时的异常pr#875@Gitee

View File

@ -127,7 +127,7 @@ public class KeyUtil {
* *
* @param algorithm 算法支持PBE算法 * @param algorithm 算法支持PBE算法
* @param keySize 密钥长度<0表示不设定密钥长度即使用默认长度 * @param keySize 密钥长度<0表示不设定密钥长度即使用默认长度
* @param random 随机数生成器null表示默认 * @param random 随机数生成器null表示默认
* @return {@link SecretKey} * @return {@link SecretKey}
* @since 5.5.2 * @since 5.5.2
*/ */
@ -140,7 +140,7 @@ public class KeyUtil {
keySize = 128; keySize = 128;
} }
if(keySize > 0){ if (keySize > 0) {
if (null == random) { if (null == random) {
keyGenerator.init(keySize); keyGenerator.init(keySize);
} else { } else {
@ -365,7 +365,7 @@ public class KeyUtil {
*/ */
public static KeyPair generateKeyPair(String algorithm) { public static KeyPair generateKeyPair(String algorithm) {
int keySize = DEFAULT_KEY_SIZE; int keySize = DEFAULT_KEY_SIZE;
if("ECIES".equalsIgnoreCase(algorithm)){ if ("ECIES".equalsIgnoreCase(algorithm)) {
// ECIES算法对KEY的长度有要求此处默认256 // ECIES算法对KEY的长度有要求此处默认256
keySize = 256; keySize = 256;
} }
@ -643,7 +643,7 @@ public class KeyUtil {
public static String getAlgorithmAfterWith(String algorithm) { public static String getAlgorithmAfterWith(String algorithm) {
Assert.notNull(algorithm, "algorithm must be not null !"); Assert.notNull(algorithm, "algorithm must be not null !");
if(StrUtil.startWithIgnoreCase(algorithm, "ECIESWith")){ if (StrUtil.startWithIgnoreCase(algorithm, "ECIESWith")) {
return "EC"; return "EC";
} }
@ -745,9 +745,8 @@ public class KeyUtil {
* @return {@link KeyStore} * @return {@link KeyStore}
*/ */
public static KeyStore readKeyStore(String type, InputStream in, char[] password) { public static KeyStore readKeyStore(String type, InputStream in, char[] password) {
KeyStore keyStore; final KeyStore keyStore = getKeyStore(type);
try { try {
keyStore = KeyStore.getInstance(type);
keyStore.load(in, password); keyStore.load(in, password);
} catch (Exception e) { } catch (Exception e) {
throw new CryptoException(e); throw new CryptoException(e);
@ -755,6 +754,21 @@ public class KeyUtil {
return keyStore; return keyStore;
} }
/**
* 获取{@link KeyStore}对象
*
* @param type 类型
* @return {@link KeyStore}
*/
public static KeyStore getKeyStore(final String type) {
final Provider provider = GlobalBouncyCastleProvider.INSTANCE.getProvider();
try {
return null == provider ? KeyStore.getInstance(type) : KeyStore.getInstance(type, provider);
} catch (final KeyStoreException e) {
throw new CryptoException(e);
}
}
/** /**
* 从KeyStore中获取私钥公钥 * 从KeyStore中获取私钥公钥
* *
@ -953,9 +967,9 @@ public class KeyUtil {
* @return RSA公钥null表示私钥不被支持 * @return RSA公钥null表示私钥不被支持
* @since 5.3.6 * @since 5.3.6
*/ */
public static PublicKey getRSAPublicKey(PrivateKey privateKey){ public static PublicKey getRSAPublicKey(PrivateKey privateKey) {
if(privateKey instanceof RSAPrivateCrtKey){ if (privateKey instanceof RSAPrivateCrtKey) {
final RSAPrivateCrtKey privk = (RSAPrivateCrtKey)privateKey; final RSAPrivateCrtKey privk = (RSAPrivateCrtKey) privateKey;
return getRSAPublicKey(privk.getModulus(), privk.getPublicExponent()); return getRSAPublicKey(privk.getModulus(), privk.getPublicExponent());
} }
return null; return null;
@ -964,12 +978,12 @@ public class KeyUtil {
/** /**
* 获得RSA公钥对象 * 获得RSA公钥对象
* *
* @param modulus Modulus * @param modulus Modulus
* @param publicExponent Public Exponent * @param publicExponent Public Exponent
* @return 公钥 * @return 公钥
* @since 5.3.6 * @since 5.3.6
*/ */
public static PublicKey getRSAPublicKey(String modulus, String publicExponent){ public static PublicKey getRSAPublicKey(String modulus, String publicExponent) {
return getRSAPublicKey( return getRSAPublicKey(
new BigInteger(modulus, 16), new BigInteger(publicExponent, 16)); new BigInteger(modulus, 16), new BigInteger(publicExponent, 16));
} }
@ -977,12 +991,12 @@ public class KeyUtil {
/** /**
* 获得RSA公钥对象 * 获得RSA公钥对象
* *
* @param modulus Modulus * @param modulus Modulus
* @param publicExponent Public Exponent * @param publicExponent Public Exponent
* @return 公钥 * @return 公钥
* @since 5.3.6 * @since 5.3.6
*/ */
public static PublicKey getRSAPublicKey(BigInteger modulus, BigInteger publicExponent){ public static PublicKey getRSAPublicKey(BigInteger modulus, BigInteger publicExponent) {
final RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent); final RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(modulus, publicExponent);
try { try {
return getKeyFactory("RSA").generatePublic(publicKeySpec); return getKeyFactory("RSA").generatePublic(publicKeySpec);
@ -993,11 +1007,12 @@ public class KeyUtil {
/** /**
* 将密钥编码为Base64格式 * 将密钥编码为Base64格式
*
* @param key 密钥 * @param key 密钥
* @return Base64格式密钥 * @return Base64格式密钥
* @since 5.7.22 * @since 5.7.22
*/ */
public static String toBase64(Key key){ public static String toBase64(Key key) {
return Base64.encode(key.getEncoded()); return Base64.encode(key.getEncoded());
} }
} }