1.2.9 获取RSA密钥增加以输入流的形式获取密钥

This commit is contained in:
maxf 2018-12-29 14:52:38 +08:00
parent f77ae05ffe
commit 13328f35f3
4 changed files with 94 additions and 2 deletions

View File

@ -17,7 +17,7 @@
### 使用
>yexuejc.base.version=1.2.8
>yexuejc.base.version=1.2.9
pom.xml
```

View File

@ -1,6 +1,20 @@
yexuejc-base 更新记录
------------------
#### version 1.2.9
**time2018-12-29 14:51:33** <br/>
**branch** master <br/>
**update** <br/>
>1. 获取RSA密钥增加以输入流的形式获取密钥
#
#### version 1.2.6
**time2018-12-21 14:58:49** <br/>
**branch** master <br/>
**update** <br/>
>1. RSA 验签增加初始化方法
#
#### version 1.2.8
**time2018-12-28 20:10:14** <br/>
**branch** master <br/>

View File

@ -6,7 +6,7 @@
<groupId>com.yexuejc.base</groupId>
<artifactId>yexuejc-base</artifactId>
<version>1.2.8</version>
<version>1.2.9</version>
<name>${project.artifactId}</name>

View File

@ -5,6 +5,7 @@ import com.yexuejc.base.util.StrUtil;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
@ -44,6 +45,21 @@ public class RSA2 {
return (RSAPublicKey) c.getPublicKey();
}
/**
* 得到公钥
*
* @param pubKeyIn 密钥文件流
* @return
* @throws CertificateException
*/
public static RSAPublicKey getPublicKey(InputStream pubKeyIn) throws CertificateException {
//通过证书,获取公钥
CertificateFactory cf = null;
cf = CertificateFactory.getInstance("X.509");
Certificate c = cf.generateCertificate(pubKeyIn);
return (RSAPublicKey) c.getPublicKey();
}
/**
* 读取JKS格式的key私钥keystore格式
*
@ -61,6 +77,23 @@ public class RSA2 {
return getPrivateKey(filepath, alias, password, "JKS");
}
/**
* 读取JKS格式的key私钥keystore格式
*
* @param priKeyIn 私钥文件流
* @param alias 证书别名
* @param password 证书密码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws IOException
* @throws CertificateException
* @throws UnrecoverableKeyException
*/
public static RSAPrivateKey getPrivateKey(InputStream priKeyIn, String alias, String password) throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
return getPrivateKey(priKeyIn, alias, password, "JKS");
}
/**
* 读取PKCS12格式的key私钥pfx格式
*
@ -78,6 +111,23 @@ public class RSA2 {
return getPrivateKey(filepath, alias, password, "PKCS12");
}
/**
* 读取PKCS12格式的key私钥pfx格式
*
* @param priKeyIn 私钥文件流
* @param alias 证书别名 可空
* @param password 证书密码
* @return
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws IOException
* @throws CertificateException
* @throws UnrecoverableKeyException
*/
public static RSAPrivateKey getPrivateKeyFromPKCS12(InputStream priKeyIn, String alias, String password) throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
return getPrivateKey(priKeyIn, alias, password, "PKCS12");
}
/**
* 读取key私钥
*
@ -114,4 +164,32 @@ public class RSA2 {
return (RSAPrivateKey) ks.getKey(alias, password.toCharArray());
}
/**
* 读取key私钥
*
* @param priKeyIn 私钥文件流
* @param alias 证书别名 可空
* @param password 证书密码
* @param type 证书格式
* @return
* @throws NoSuchAlgorithmException
* @throws KeyStoreException
* @throws IOException
* @throws CertificateException
* @throws UnrecoverableKeyException
*/
public static RSAPrivateKey getPrivateKey(InputStream priKeyIn, String alias, String password, String type) throws NoSuchAlgorithmException, KeyStoreException, IOException, CertificateException, UnrecoverableKeyException {
KeyStore ks = KeyStore.getInstance(type);
ks.load(priKeyIn, password.toCharArray());
if (StrUtil.isEmpty(alias)) {
Enumeration<?> aliases = ks.aliases();
if (aliases != null) {
if (aliases.hasMoreElements()) {
alias = (String) aliases.nextElement();
}
}
}
return (RSAPrivateKey) ks.getKey(alias, password.toCharArray());
}
}