密钥文件读取保存为String,并转PublicKeyPrivateKey

密钥⽂件读取保存为String,并转PublicKeyPrivateKey
开发中得到密钥⽂件,需要将密钥获取出来,转为String,然后存储在数据库中,后⼜需要获取然后转为PublicKey/PrivateKey使⽤,以下是针对⼀种农⾏公钥私钥类型的密钥做转换,不⼀定通⽤其他,因为还没试过
获取.cer公钥⽂件并读取
/********************************
*
* @Description  获取.cer公钥⽂件并读取
* @MethodName  readPublicKey
* @param        file
* @return      java.lang.String
* @Author      fancw
* @Date        2019/1/4  9:30
*
*******************************/
private static String readPublicKey(File file){
String publicKeyString = null;
// 从指定流中获取数据并⽣成证书
X509Certificate cert = null;
try{
CertificateFactory cf = Instance("X.509");
cert =(X509Certificate) cf.generateCertificate(new FileInputStream(file));
PublicKey publicKey = PublicKey();
System.out.println("-----------------获取的公钥--------------------");
System.out.println(publicKey);
publicKeyString =new Encoded()));
System.out.println("-----------------Base64encode后的公钥--------------------");
System.out.println(publicKeyString);
}catch(CertificateException e){
e.printStackTrace();
}catch(FileNotFoundException e){
e.printStackTrace();
}
return publicKeyString;
}
获取.pfx⽂件并读取
/********************************
*
* @Description  获取.pfx⽂件并读取
* @MethodName  readMerchantKey
* @param        map
* @param        file
* @param        keystorePassword
* @return      java.util.HashMap<java.lang.String,java.lang.String>
* @Author      fancw
* @Date        2019/1/4  9:32
*
*******************************/
private static HashMap<String, String>readMerchantKey(HashMap<String, String> map, File file, String keystorePassword){
try{
KeyStore ks = Instance("PKCS12");
FileInputStream fis =new FileInputStream(file);
char[] nPassword = null;
if(StringUtil.isEmpty(keystorePassword)){
nPassword = null;
}else{
nPassword = CharArray();
nPassword = CharArray();
}
// 从指定输⼊流中加载 KeyStore
ks.load(fis, nPassword);
fis.close();
System.out.println("keystore type="+ ks.getType());
// 获取密钥库的所有别名
Enumeration<String> enums = ks.aliases();
String keyAlias = null;
// 测试此枚举是否包含更多的元素
if(enums.hasMoreElements())
{
// 如果此枚举对象⾄少还有⼀个可提供的元素,则返回此枚举的下⼀个元素
keyAlias =(String) Element();
System.out.println("alias=["+ keyAlias +"]");
}
// 判断给定别名是否通过调⽤ setKeyEntry
//  或者以 privateKeyEntry 或 SecretKeyEntry 为参数 setEntry 创建的
System.out.println("is key entry="+ ks.isKeyEntry(keyAlias));
// 获取和别名绑定的密钥,并⽤给定密码来恢复它
PrivateKey prikey =(PrivateKey) ks.getKey(keyAlias, nPassword);
/
/ 获取和别名绑定的证书
// Certificate / 管理各种⾝份证书
// ⾝份证书是⼀个主体与由另⼀个主体所担保的公钥之间的绑定关系
Certificate cert = ks.getCertificate(keyAlias);
// 从证书中获取公钥
PublicKey pubkey = PublicKey();
// 获取证书名
//            System.out.println("cert class = " + Class().getName());
//            System.out.println("cert = " + cert);
//            System.out.println("public key = " + pubkey);
//            System.out.println("private key = " + prikey);
System.out.println("-----------------公钥--------------------");
String pubkeyKeyStr =new Encoded()));            System.out.println(pubkeyKeyStr);
System.out.println("-----------------公钥--------------------");
System.out.println("-----------------私钥--------------------");
String privateKeyStr =new Encoded()));            System.out.println(privateKeyStr);
System.out.println("-----------------私钥--------------------");
map.put("密钥【"+ Name()+"】 - 公钥", pubkeyKeyStr);
map.put("密钥【"+ Name()+"】 - 私钥", privateKeyStr);
map.put("密钥【"+ Name()+"】 - 密码", keystorePassword);
}catch(KeyStoreException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}catch(CertificateException e){
e.printStackTrace();
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(UnrecoverableKeyException e){
e.printStackTrace();
}
return map;
}
将数据库中读取的str公钥转为 PublicKey
/********************************
*
* @Description  将数据库中读取的str公钥转为 PublicKey
* @MethodName  test
* @param        publicKeyString
* @return      java.security.PublicKey
* @Author      fancw
* @Date        2019/1/3  9:34
*
*******************************/
private static PublicKey strToPublicKey(String publicKeyString){
PublicKey publicKey = null;
try{
java.security.spec.X509EncodedKeySpec bobPubKeySpec =new java.security.spec.X509EncodedKeySpec( new BASE64Decoder().decodeBuffer(publicKeyString));
java.security.KeyFactory keyFactory = java.Instance("RSA");
// 取公钥匙对象
publicKey = atePublic(bobPubKeySpec);
}catch(InvalidKeySpecException e){
e.printStackTrace();
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return publicKey;
}
将数据库中读取的str私钥转为 PrivateKey
/********************************
*
* @Description  将数据库中读取的str私钥转为 PrivateKey
* @MethodName  strToPrivateKey
* @param        privateKeyString
* @return      java.security.PrivateKey
* @Author      fancw
* @Date        2019/1/3  11:24
*
*******************************/
private static PrivateKey strToPrivateKey(String privateKeyString){
PrivateKey privateKey = null;
try{
byte[] keyBytes =new byte[0];
keyBytes =(new BASE64Decoder()).decodeBuffer(privateKeyString);
PKCS8EncodedKeySpec pkcs8EncodedKeySpec =new PKCS8EncodedKeySpec(keyBytes);
// 指定加密算法
KeyFactory keyFactory = Instance("RSA");
// 取私钥匙对象
privateKey = atePrivate(pkcs8EncodedKeySpec);
}catch(InvalidKeySpecException e){
e.printStackTrace();
}catch(NoSuchAlgorithmException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
return privateKey;
}
避免⽅法乱⽤,⽬前也只是适⽤于项⽬某特殊位置,所以⽅法全为 private

本文发布于:2024-09-21 00:50:14,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/92353.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:密钥   公钥   读取   获取   转为   是否
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议