通过 openSSL API 加载从 java 生成的公钥
应该使用 JCE 实施解决方案 我有一个使用 KeyPairGenerator 生成的公钥字符串。
如何使用 PEM_read_bio_RSAPublicKey 在 openSSL Api 调用中加载它?或者这将具有 x509 规范编码,我们如何删除和生成与 openssl api 调用兼容的公共?
try {
// Get the public/private key pair
KeyPairGenerator keyGen = KeyPairGenerator
.getInstance(keyAlgorithm);
keyGen.initialize(numBits);
KeyPair keyPair = keyGen.genKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
System.out.println("\n" + "Generating key/value pair using "
+ privateKey.getAlgorithm() + " algorithm");
// Get the bytes of the public and private keys
privateKeyBytes = privateKey.getEncoded();
publicKeyBytes = publicKey.getEncoded();
try {
rsa_publickey = new
BASE64Encoder().encodeBuffer((keyPair.getPublic()).getEncoded());
}
catch(Exception e1)
{
e1.printStackTrace();
}
System.out.println("PublicKey :"+rsa_publickey);
}
catch(Exception e1)
{
e1.printStackTrace();
}
Should implement the solution with JCE
I Have a Public Key String generated using the KeyPairGenerator.
How do I load this in openSSL Api call using PEM_read_bio_RSAPublicKey? Or this will have the x509 spec encoded how do we remove and generate the public which is compatible with the openssl api call?
try {
// Get the public/private key pair
KeyPairGenerator keyGen = KeyPairGenerator
.getInstance(keyAlgorithm);
keyGen.initialize(numBits);
KeyPair keyPair = keyGen.genKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
System.out.println("\n" + "Generating key/value pair using "
+ privateKey.getAlgorithm() + " algorithm");
// Get the bytes of the public and private keys
privateKeyBytes = privateKey.getEncoded();
publicKeyBytes = publicKey.getEncoded();
try {
rsa_publickey = new
BASE64Encoder().encodeBuffer((keyPair.getPublic()).getEncoded());
}
catch(Exception e1)
{
e1.printStackTrace();
}
System.out.println("PublicKey :"+rsa_publickey);
}
catch(Exception e1)
{
e1.printStackTrace();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许您可以尝试使用 Bouncycastle 的 Bouncycastle 将其从 Java 导出为 OpenSSL 可以读取的 PEM 格式="http://www.bouncycastle.org/docs/docs1.6/org/bouncycastle/openssl/PEMWriter.html" rel="nofollow">
PEMWriter
。编辑: 例如,以下代码:
产生此输出:(
如果您想在没有 BouncyCastle 的情况下执行此操作,您可能需要使用另一个 Base 64 编码器,因为通常不建议使用
sun.*
软件包可能不会在所有 JRE 上公开或可用。)我没有尝试使用 OpenSSL 的 API 加载,但是在命令行上使用 OpenSSL,当您粘贴上面的密钥时,您会得到这个(请注意,BEGIN/END 分隔符之间的内容是标准输入,粘贴在此处的终端上):
编辑:
如果您想导出
BEGIN RSA PUBLIC KEY
中的某些内容,您可以尝试如下操作:Perhaps you could try to export it from Java to the PEM format OpenSSL can read, using Bouncycastle's
PEMWriter
.EDIT: For example, the following code:
produces this output:
(If you want to do it without BouncyCastle, you might want to use another base 64 encoder, since it's usually not recommended to use
sun.*
packages that may not be exposed or available on all JREs.)I haven't tried to load in using OpenSSL's API, but with OpenSSL on the command line, when you paste the above key, you get this (note that what's between the BEGIN/END delimiters is stdin, pasted on the terminal here):
EDIT:
If you want to export something within the
BEGIN RSA PUBLIC KEY
instead, you can try something like this: