@SpringBootApplication
public class DemojwtApplication {
// 寻找证书文件
private static InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("token.jks");
private static PrivateKey privateKey = null;
private static PublicKey publicKey = null;
public static void main(String[] args) {
SpringApplication.run(DemojwtApplication.class, args);
System.out.println("hello");
try {
KeyStore keyStore = KeyStore.getInstance("JKS");
keyStore.load(inputStream, "密码".toCharArray());
privateKey = (PrivateKey) keyStore.getKey("key的别名", "密码".toCharArray());
System.out.println(encryptBASE64(privateKey.getEncoded()));
publicKey = keyStore.getCertificate("key的别名").getPublicKey();
System.out.println(encryptBASE64(publicKey.getEncoded()));
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
//编码返回字符串
public static String encryptBASE64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}
}
从JWT.jks中读取公钥与私钥
最新推荐文章于 2025-03-03 17:14:14 发布
本文详细讲解如何使用Java从JWT的.jks密钥库文件中提取公钥和私钥,这对于验证JWT令牌和签署新的JWT至关重要。通过Java KeyStore API,你可以轻松地管理和使用这些密钥。
5980

被折叠的 条评论
为什么被折叠?



