In the end you'll have to program it I suppose, here's a small Java application that creates a PEM encoded RSA PRIVATE KEY (unencrypted PKCS#1 structure) and PUBLIC KEY (X.509 SubjectPublicKeyInfo).
Obviously it is build on top of the Bouncy Castle provider for Java / JCA and the PemWriter
from the "lightweight" API that is part of the provider package.
package com.stackexchange.crypto;
import java.io.FileWriter;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Security;
import org.bouncycastle.asn1.ASN1Object;
import org.bouncycastle.asn1.ASN1Sequence;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemWriter;
public class ShortRsaKeyPair {
public static void main(String[] args) throws Exception{
Security.addProvider(new BouncyCastleProvider());
KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA", "BC");
kpg.initialize(228);
KeyPair kp = kpg.generateKeyPair();
PrivateKeyInfo privKeyInfo = PrivateKeyInfo.getInstance(ASN1Sequence.getInstance(kp.getPrivate().getEncoded()));
ASN1Object pkcs1Object = (ASN1Object) privKeyInfo.parsePrivateKey();
try (PemWriter pemWriter = new PemWriter(new FileWriter("privkey.pem"))) {
pemWriter.writeObject(new PemObject("RSA PRIVATE KEY", pkcs1Object.getEncoded("DER")));
}
// not needed, you can also generate it from the private key using "openssl rsa -pubout"
try (PemWriter pemWriter = new PemWriter(new FileWriter("pubkey.pem"))) {
pemWriter.writeObject(new PemObject("PUBLIC KEY", kp.getPublic().getEncoded()));
}
}
}
You can check the result using:
openssl rsa -pubin -in pubkey.pem -text
openssl rsa -in privkey.pem -text