

使用 java 提取 jks 文件中的公钥和私钥
source link: https://fanlumaster.github.io/2023/09/02/%E4%BD%BF%E7%94%A8-java-%E6%8F%90%E5%8F%96-jks-%E6%96%87%E4%BB%B6%E4%B8%AD%E7%9A%84%E5%85%AC%E9%92%A5%E5%92%8C%E7%A7%81%E9%92%A5/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

使用 java 提取 jks 文件中的公钥和私钥
使用 java 提取 jks 文件中的公钥和私钥 _
2.1k 字
18 分钟
注意,这个代码只能提取标准 jks 文件中的公钥和私钥,如果是使用 jdk17 中的 keytool 生成的 jks 文件,本文的代码是不生效的。
我们用 jdk17 版本的 keytool 生成的密钥库类型为 PKCS12,而 PKCS12 只有 storepass 属性,没有 keypass,因此我们单独指定密钥条目的密码,即填入的 keypass 将不会生效,签名的时候也会无法正常按照我们创建时填写的 keypass 来读取我们的密钥。
我这里使用的 jks 文件是使用 jdk8 中的 keytool 生成的。
代码写出来很简单,
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import java.io.FileInputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.cert.Certificate;
import java.util.Base64;
public class KeyStoreExtract {
public static void main(String[] args) {
String jksFile = "C:\\EDisk\\JavaCodes\\certificate\\jks_demo\\fanyfull.jks";
String alias = "fanyfull";
String storepass = "ffsp123456";
String keypass = "ffkp123456";
try {
FileInputStream fis = new FileInputStream(jksFile);
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(fis, storepass.toCharArray());
fis.close();
Security.addProvider(new BouncyCastleProvider());
PrivateKey privateKey = (PrivateKey) keyStore.getKey(alias, keypass.toCharArray());
Certificate cert = keyStore.getCertificate(alias);
PublicKey publicKey = cert.getPublicKey();
String base64PrivateKey = Base64.getEncoder().encodeToString(privateKey.getEncoded());
String base64Cert = Base64.getEncoder().encodeToString(cert.getEncoded());
String base64PublicKey = Base64.getEncoder().encodeToString(publicKey.getEncoded());
// public key
System.out.println("-----BEGIN PUBLIC KEY-----");
System.out.println(base64PublicKey);
System.out.println("-----END PUBLIC KEY-----");
// private key
System.out.println("-----BEGIN PRIVATE KEY-----");
System.out.println(base64PrivateKey);
System.out.println("-----END PRIVATE KEY-----");
// cert
System.out.println("-----BEGIN CERTIFICATE-----");
System.out.println(base64Cert);
System.out.println("-----END CERTIFICATE-----");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Recommend
-
105
最近一段时间,国内各大网站纷纷用上了https连接,在访问这些网站的时候,很多浏览器会给予“特别关照”,给它们的链接旁边加上一个绿色的小锁,那么,什么是https,它与网络安全又有什么关系呢?今天我们就来谈谈https与tls(传输层安全)。
-
77
程序员 - @GuguguguDa - 下面这个博客看的我很疑惑啊先不论私钥加密,公钥解密有没有意义私钥加密的结果,用公钥解密,还能解出来的吗?我试了下,居然还真可以?https://blog.csdn.net/
-
25
以太坊geth 公私钥地址签名转换基础 以太坊中使用的椭圆曲线是S256,私钥和HASH都为32位,公钥为65位其中第一位是压缩字节0x04,压缩公钥为33字节,地址是是公钥的后64位hash后取后20个字节作为地址。签名数据为65位,R,S...
-
7
零一间2021.04.20 01:16:38字数 1,347阅读 29理解公钥与私钥 一、公钥算法与私钥算法 1、私钥算法 ...
-
28
web3js里有直接从私钥获取公钥的接口吗 是这个web3.shh.getPublicKey吗,我调用直接报错没有这类方法 ...
-
6
在以太坊中,账户、地址、私钥(Private Key)和公钥(Public Key)是非常重要的概念。账户扮演着以太坊的中心角色,地址是我们与以太坊系统进行交互的标识,它是以太坊账户与外界进行交互的名字,而私钥与公钥是保护我们账户安全的重要屏障。 什么是...
-
8
March 22, 2021 一般来说,加密主要用于消息的传递,并且传递的消息只有持有私钥的那个人能解密。因此, 像 RSA 这种非对称密钥加密算法的常用场景是:对于加密,公钥加密,私钥解密。为什么不是 私钥加密...
-
4
runliuv runliuv@cnblogs
-
6
jdk 中的 keytool 的使用,以及提取 jks 文件中的公钥和私钥 Full's Blog
-
4
Java中从文件路径字符串中提取文件扩展名3种方法 在
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK