help making private key base 64 when generating priv key
843811May 31 2007 — edited Jun 1 2007Hi,
I want to provide a program that will generate a certificate request (CSR) and a private key, without using a java keystore. I was able to generate the CSR fine and persisted the CSR in base64 format to a file. But I'm having trouble generating the private key and having it store in a base 64 format. When I generate the private key it never prompt me for a password, so I assume my code below doesn't have a passwd on the private key? Any help of storing the private key encoded base64 is appreciated!
public static void generatePKCS10() throws Exception {
// generate PKCS10 certificate request
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
String sigAlg = "MD5WithRSA";
// generate private key - use java.util.SecureRandom for entropy
keyGen.initialize(1024, new SecureRandom());
KeyPair keypair = keyGen.generateKeyPair();
PublicKey publicKey = keypair.getPublic();
PrivateKey privateKey = keypair.getPrivate();
//Saving private key to a file; private key format is wrong not base 64
File file = new File("c:\\testkey.pem");
FileOutputStream fos = new FileOutputStream(file);
PrintStream pstream = new PrintStream(fos);
pstream.print(privateKey);
PKCS10 pkcs10 = new PKCS10(publicKey);
Signature signature = Signature.getInstance(sigAlg);
signature.initSign(privateKey);
//common, orgUnit, org, locality, state, country
X500Name x500Name = new X500Name(
"Joe", // CN
"Development", // OU
"Test", // O
"Berkeley", // L
"CA", // S
"US"); // C
pkcs10.encodeAndSign(new X500Signer(signature, x500Name));
/* save the csr key in a file */
File f = new File("c:\\testcsr.pem");
FileOutputStream keyfos = new FileOutputStream(f);
PrintStream ps = new PrintStream(keyfos);
pkcs10.print(ps);
keyfos.close();
pkcs10.print(System.out);
}
The content of the testkey.pem (private key) below, I want it to be base 64:
Sun RSA private CRT key, 1024 bits
modulus: 119018932003181102693783448669560193625436989425741056741549401505834669765892873575333831437015798039523628802932886701805854708164082950475820297699361469065371448644559928540183516883541041877747208838603430995901338687932368965056164689685466625751777136200108923762036012886953379835389315531602272567077
public exponent: 65537
private exponent: 48612215570763870133011815523853506614060718875570084179284898294218265106633206278354761431039548376061896269235813528753820266843709239335592989133108125464712151632582672201298744273324199837032373092112928877723659680910255584904173733544535211614358250462053923293158754267918360737753533103016681521857
prime p: 11663880968052493234750207640699246098576187391600573539297343873417159073703938874946076968061314727219871574003407784844424106433281176461204586128578299
9934661539989373932628545675007375310461385774643960289413146591165048914201334643516401021415922973514327286071729302438513320606178376146394754117239869