原文地址:https://stackoverflow.com/questions/7611383/generating-rsa-keys-in-pkcs1-format-in-java
When I generate an RSA key pair using the Java API, the public key is encoded in the X.509 format and the private key is encoded in the PKCS#8 format. I'm looking to encode both as PKCS#1. Is this possible? I've spent a considerable amount of time going through the Java docs but haven't found a solution. The result is the same when I use the Java and the Bouncy Castle providers.
Here is a snippet of the code:
KeyPairGeneratorkeygen=KeyPairGenerator.getInstance("RSA","BC");keygen.initialize(1024);KeyPairpair=keygen.generateKeyPair();PrivateKeypriv=pair.getPrivate();PublicKeypub=pair.getPublic();byte[]privBytes=priv.getEncoded();byte[]pubBytes=pub.getEncoded();
The two resulting byte arrays are formatted as X.509 (public) and PKCS#8 (private).
Any help would be much appreciated. There are some similar posts but none really answer my question.
Thank You
You will need BouncyCastle:
importorg.bouncycastle.asn1.ASN1Encodable;importorg.bouncycastle.asn1.ASN1Primitive;importorg.bouncycastle.asn1.pkcs.PrivateKeyInfo;importorg.bouncycastle.asn1.x509.SubjectPublicKeyInfo;importorg.bouncycastle.util.io.pem.PemObject;importorg.bouncycastle.util.io.pem.PemWriter;
The code snippets below have been checked and found working with Bouncy Castle 1.52.
Private key
Convert private key from PKCS8 to PKCS1:
PrivateKeypriv=pair.getPrivate();byte[]privBytes=priv.getEncoded();PrivateKeyInfopkInfo=PrivateKeyInfo.getInstance(privBytes);ASN1Encodable encodable=pkInfo.parsePrivateKey();ASN1Primitive primitive=encodable.toASN1Primitive();byte[]privateKeyPKCS1=primitive.getEncoded();
Convert private key in PKCS1 to PEM:
PemObjectpemObject=newPemObject("RSA PRIVATE KEY",privateKeyPKCS1);StringWriterstringWriter=newStringWriter();PemWriterpemWriter=newPemWriter(stringWriter);pemWriter.writeObject(pemObject);pemWriter.close();StringpemString=stringWriter.toString();
Check with command line OpenSSL that the key format is as expected:
openssl rsa-in rsa_private_key.pem-noout-text
Public key
Convert public key from X.509 SubjectPublicKeyInfo to PKCS1:
PublicKeypub=pair.getPublic();byte[]pubBytes=pub.getEncoded();SubjectPublicKeyInfospkInfo=SubjectPublicKeyInfo.getInstance(pubBytes);ASN1Primitive primitive=spkInfo.parsePublicKey();byte[]publicKeyPKCS1=primitive.getEncoded();
Convert public key in PKCS1 to PEM:
PemObjectpemObject=newPemObject("RSA PUBLIC KEY",publicKeyPKCS1);StringWriterstringWriter=newStringWriter();PemWriterpemWriter=newPemWriter(stringWriter);pemWriter.writeObject(pemObject);pemWriter.close();StringpemString=stringWriter.toString();
Check with command line OpenSSL that the key format is as expected:
openssl rsa-in rsa_public_key.pem-RSAPublicKey_in-noout-text
Thanks
Many thanks to the authors of the following posts:
https://stackoverflow.com/a/8713518/1016580
https://stackoverflow.com/a/14052651/1016580
https://stackoverflow.com/a/14068057/1016580
Those posts contained useful, though sometimes outdated info (i.e. for older versions of BouncyCastle), that helped me to construct this post.
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2730/viewspace-3166620/ ,如需转载,请注明出处,否则将追究法律责任。