help making private key base 64 when generating priv key

Hi,

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

[3113 byte] By [cup_joea] at [2007-11-27 6:05:09]
# 1

Are you just "practicing" crypto-coding or are you planning to implement this design into production? If it is the latter, you should think about using industry standards rather than a Base64-encoded file for the Private Key. If you do not want to use a Java keystore, then a PKCS#8 file (http://www.rsa.com/rsalabs/node.asp?id=2130) which stores cryptographic private keys protected by passwords is the industry norm.

If you want to use a ready-made, open-source tool for this purpose, you will find CSRTool at http://www.strongauth.com/index.php?option=com_content&task=view&id=32&Itemid=32 or http://sourceforge.net/projects/csrtool.

arshad.noora at 2007-7-12 16:50:41 > top of Java-index,Security,Cryptography...
# 2

Hi arshad,

Thanks for the response, yes I am using this for production. The reason I need the private key in pem format is because our ssl application read the private key in this format.

When I check the private key I have it is in pkcs8 format:

Generated Private Key:: PKCS#8

The pkcs8 I have persisted looks like:

Sun RSA private CRT key, 1024 bits

modulus: 90777718180030692527922784689709891071

public exponent: 65537

private exponent: 23804963068831461339165371891868016356570783040671

prime p: 13065536726714291965404605205289282570354428171395

prime exponent q: 36266515345812499992491217727803750986765090305298

cup_joea at 2007-7-12 16:50:41 > top of Java-index,Security,Cryptography...