diff --git a/README.md b/README.md index db6ecaa..8f14f65 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ ### 使用 ->yexuejc.base.version=1.2.8 +>yexuejc.base.version=1.2.9 pom.xml ``` diff --git a/UPDATE.md b/UPDATE.md index aff1f9f..7e601bd 100644 --- a/UPDATE.md +++ b/UPDATE.md @@ -1,6 +1,20 @@ yexuejc-base 更新记录 ------------------ +#### version :1.2.9 +**time:2018-12-29 14:51:33**
+**branch:** master
+**update:**
+>1. 获取RSA密钥增加以输入流的形式获取密钥 + +# +#### version :1.2.6 +**time:2018-12-21 14:58:49**
+**branch:** master
+**update:**
+>1. RSA 验签增加初始化方法 + +# #### version :1.2.8 **time:2018-12-28 20:10:14**
**branch:** master
diff --git a/pom.xml b/pom.xml index 791015d..aa6f90f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.yexuejc.base yexuejc-base - 1.2.8 + 1.2.9 ${project.artifactId} diff --git a/src/main/java/com/yexuejc/base/encrypt/RSA2.java b/src/main/java/com/yexuejc/base/encrypt/RSA2.java index 20f516b..2e86afd 100644 --- a/src/main/java/com/yexuejc/base/encrypt/RSA2.java +++ b/src/main/java/com/yexuejc/base/encrypt/RSA2.java @@ -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()); + } + }