将公钥(.pem)转换为JWK
我已经使用openSSL创建了一个私有/公共密钥对,但是我需要将公共密钥转换为JWK。
我可以通过openssl做到这一点吗?还是可以通过在线转换器进行安全操作?
目前仅用于测试目的,但是我需要在某个时候进行生产,并且在线转换器似乎对生产似乎不是很安全。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用在线转换器。可能的危险是:
恶意网站代替您的公共关键,以欺骗您接受其(false)签名或加密他们可以阅读的数据。为了防止这种情况,只需在使用返回的publicKey进行生产之前测试返回的publicKey:它应该验证使用您的私人关键或加密与私人关键的数据进行的签名。对于JWK(和Jose)支持的所有不对称算法,如果它与您的私人关键一起使用,则无法与任何对手私有键一起使用。
网站变得不可用,无论是偶然的(服务器崩溃,网络问题)还是故意(赞助商停止操作,或更改策略以使其无法使用,例如收取高费用)。除了相信互联网是如此“真的很大”(例如道格·亚当斯的空间)以外,总会有人在某个地方提供您需要的东西。
,没有任何保证。
如果您认为将公共关键披露到网站,甚至像ISP一样向网站上的其他人披露是危险的,则不是。 PublicKey密码学的重点和理由是,包括对手在内的任何人和所有人都可以知道公共关键。
但是,这不是编程或开发,也是如此。
自己做。 从JWK生成公共密钥下半场 - 如果您有Java的内部格式(JCA对象)中的键,Nimbusds库可以将其导出到JWK。 (它可以类似地从JWK导入到JCA,但您不需要。它处理公共或私钥。)在上半年,从OpenSSL文件读取到JCA,这取决于许多OpenSSL格式中的哪个您使用(D),您的Q未指定或描述。
如果使用openssl创建一个证书包含 publicKey,则以pem或'der'(二进制)格式创建java的
centerripfactory
可以读取该;请参阅 javadoc 。 (Java将笨拙地描述为base64,由-----开始/结束证书------
行是OpenSSL,几乎所有其他人都称为PEM。)如果您使用OpenSSL来使用任何一个
(或调用任何
i2d_*pubkey*
api)创建一个单独的PubKey der文件,Java可以直接使用
X509encodedKeyspec
包含文件内容(读取为字节而非字符,例如。参见各自的Javadocs。如果使用openssl创建一个单独的PEM文件,例如上述使用
-Outform der
省略的,则需要自己处理PEM部分,但这并不难:阅读文件,请删除该文件-----(开始| End)公共密钥-----
行,base64-decode剩下的除线路折扣以外 - 删除线路断开,然后解码,或使用java.lang.base64.getMimedeCoder()
,忽略了linebreaks 时 解码。然后按照上方进行操作。另外,如果您有/从 pemparser 和jcapemkeyconverter
搜索许多现有QS-但我认为对于这种情况,这是最简单的情况之一。如果您使用openssl来创建(仅)a privateKey 文件,它实际上包含创建publicKey所需的信息,但使用它可能更难。首先,至少有7个,有时有更多格式可以使用OpenSSL,只有2个Java可以轻松读取,即使您可以阅读将其转换为公共的RSA(因为OpenSSL都使用由CRT-Form Key定义的CRT形式) PKCS1和JCA也支持这一点),但对于EC来说很难。由于这里的可能性范围要大得多,而且更复杂,所以除非您指定需要它,否则我不会尝试覆盖它,以及确切的部分。
Using online converter. The possible dangers are:
malicious site substitutes their publickey for yours, in an attempt to trick you into accepting their (false) signatures or encrypting data they can read. To prevent this, just test the returned publickey before using it for production: it should verify a signature made with your privatekey or encrypt data that decrypts with your privatekey. For all asymmetric algorithms supported by JWK (and JOSE), if it works with your privatekey it doesn't work with any adversary privatekey.
site becomes unavailable, either accidentally (server crash, network problem) or intentionally (sponsor ceases operation, or changes policy to make it unusable for you such as charging high fees). There isn't really any guarantee against this except the belief that the Internet is so "really really big" (like Doug Adams' space) that there will always be someone somewhere offering what you need.
In case you thought disclosure of the publickey to the site or even to others on the network like your ISP is a danger, it isn't. The whole point of and reason for publickey cryptography is that a publickey can be known to anyone and everyone including even adversaries.
However, this is not programming or development and is offtopic for SO.
Doing it yourself. Generating Public Key from JWK gives you the second half -- if you have a key in Java's internal format (a JCA object) that nimbusds library can export it to JWK. (It can similarly import from JWK to JCA, but you don't need that. And it handles either public or private keys.) For the first half, reading from an OpenSSL file to JCA, it depends on which of the many OpenSSL formats you use(d), which your Q didn't specify or describe.
If you use openssl to create a certificate containing the publickey, in either PEM or 'DER' (binary) format, Java's
CertificateFactory
can read that; see the javadoc. (What Java describes clumsily as base64 bounded by-----BEGIN/END CERTIFICATE-----
lines is what OpenSSL and nearly everybody else in the world calls PEM.)If you use openssl to create a separate pubkey DER file with any of
(or a program that calls any
i2d_*PUBKEY*
API),Java can handle that directly with
X509EncodedKeySpec
containing the contents of the file (read as bytes not chars, e.g.java.nio.file.Files#readAllBytes
) passed to.generatePublic
on aKeyFactory
for the relevant algorithm ("RSA"
or"EC"
); see the respective javadocs.If you use openssl to create a separate PEM file, such as the above with
-outform der
omitted, you need to handle the PEM part yourself, but it isn't hard: read the file, remove the-----(BEGIN|END) PUBLIC KEY-----
lines, and base64-decode the rest except the linebreaks -- either remove the linebreaks and then decode, or usejava.lang.Base64.getMimeDecoder()
which ignores the linebreaks while decoding. Then proceed as for DER above. Alternatively if you have/get the third-party libraries from https://www.bouncycastle.org they can handle this format directly -- search for many existing Qs usingPEMParser
andJcaPEMKeyConverter
-- but I consider Bouncy overkill for this case which is one of the simplest.If you use openssl to create (only) a privatekey file, it actually contains the information needed to create the publickey, but using it may be harder. First of all there are at least 7 and sometimes more formats OpenSSL can use, only 2 of which Java can easily read, and even if you can read it converting to public is easy for RSA (because OpenSSL uses the CRT-form key defined by PKCS1, and JCA also supports this) but hard for EC. Because the range of possibilities here is much larger and more complicated, I'm not going to try to cover it unless you specify that you need it, and exactly which part(s) of it.