'Which openssl keypair gen command produces a password-less private key that I can read in Java 17?
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...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|