哪个openssl keypair gen命令会产生我可以在Java 17中阅读的无密码的私钥?

发布于 2025-01-28 09:55:12 字数 1539 浏览 3 评论 0原文

在这一天中,最好的一天燃烧了。 Google说,使用PKCS#8格式作为私钥,因此

$ openssl genpkey -out my.prv.key -algorithm RSA -pkeyopt rsa_keygen_bits:2048
# from https://www.feistyduck.com/library/openssl-cookbook/online/ch-openssl.html

我尝试使用Kotlin代码

    val stringKey = Files.readString(Path.of("/Users/matthewadams/my.prv.key"))
    val keyFactory = KeyFactory.getInstance("RSA")
    val privateKeySpec = PKCS8EncodedKeySpec(stringKey.toByteArray())
    val key = keyFactory.generatePrivate(privateKeySpec)

i wally effere werferal 获得了一个例外

Caused by: java.security.InvalidKeyException: invalid key format
    at java.base/sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:103)
    at java.base/sun.security.pkcs.PKCS8Key.<init>(PKCS8Key.java:95)
    at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:153)
    at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:90)
    at java.base/sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:352)
    at java.base/sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:249)
    ... 121 more

时, 我发布了命令(Alpine openssl 1.1.1o)。 - 使用openssl(在MacOS w/libressl或Alpine w/openssl上使用openssl)的无RSA专用密钥,可以使用运行Java 17的Vanilla Kotlin代码读取?

无论我尝试什么变化,都没有任何可起步。谷歌谷歌无济于事。我觉得我缺少一些简单的东西。甚至尝试剥离-----开始私有密钥-----&amp; -----结束私钥------ - 似乎无效...

Burned better part of a day on this. Google says use PKCS#8 format for the private key, so I issued command (alpine openssl 1.1.1o)

$ openssl genpkey -out my.prv.key -algorithm RSA -pkeyopt rsa_keygen_bits:2048
# from https://www.feistyduck.com/library/openssl-cookbook/online/ch-openssl.html

When I try to read this with Kotlin code

    val stringKey = Files.readString(Path.of("/Users/matthewadams/my.prv.key"))
    val keyFactory = KeyFactory.getInstance("RSA")
    val privateKeySpec = PKCS8EncodedKeySpec(stringKey.toByteArray())
    val key = keyFactory.generatePrivate(privateKeySpec)

I always get an exception

Caused by: java.security.InvalidKeyException: invalid key format
    at java.base/sun.security.pkcs.PKCS8Key.decode(PKCS8Key.java:103)
    at java.base/sun.security.pkcs.PKCS8Key.<init>(PKCS8Key.java:95)
    at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.<init>(RSAPrivateCrtKeyImpl.java:153)
    at java.base/sun.security.rsa.RSAPrivateCrtKeyImpl.newKey(RSAPrivateCrtKeyImpl.java:90)
    at java.base/sun.security.rsa.RSAKeyFactory.generatePrivate(RSAKeyFactory.java:352)
    at java.base/sun.security.rsa.RSAKeyFactory.engineGeneratePrivate(RSAKeyFactory.java:249)
    ... 121 more

How do I generate a password-less RSA private key using openssl (on macos w/LibreSSL or alpine w/openssl) that can be read using vanilla Kotlin code running Java 17?

No matter what variation on this I try, nothing works. Googled a ton to no avail. I feel like I'm missing something simple. Even tried stripping off -----BEGIN PRIVATE KEY----- & -----END PRIVATE KEY----- -- nothing seems to work...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文