View Single Post
  #1 (permalink)  
Old 10-30-06, 10:53 AM
coolcoder coolcoder is offline
Newbie Coder
 
Join Date: May 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
creating a digital certificate using java security or bouncy castle

creating a digital certificate using java security or bouncy castle..

i am working in java security..
i need to create a digital certificate using java..
there r tools like keytool

but i need to do using programming in java...
1.doubt 1:

is it possible to create a digital certificate using java security packages alone?
with the present api,i am only able to parse the exisiting certificate...
& not able to create a new one...


doubt 2:

i used the bouncy castle api to create the certificate but it deosnt accept teh signature alogirthm i give...

i tried to give rsa,dsa,etc...
all didnt work out..

this is my code:
Code:
package chapter6;

import java.math.BigInteger;
import java.security.*;
import java.security.cert.X509Certificate;
import java.util.Date;

import javax.security.auth.x500.X500Principal;

import org.bouncycastle.x509.X509V1CertificateGenerator;


/**
 * Basic X.509 V1 Certificate creation.
 */
public class X509V1CreateExample
{
    public static X509Certificate generateV1Certificate(KeyPair pair)
        throws InvalidKeyException, NoSuchProviderException, SignatureException
    {
        // generate the certificate
        X509V1CertificateGenerator  certGen = new X509V1CertificateGenerator();

        certGen.setSerialNumber(BigInteger.valueOf(System.currentTimeMillis()));
        certGen.setIssuerDN(new X500Principal("CN=Test Certificate"));
        certGen.setNotBefore(new Date(System.currentTimeMillis() - 50000));
        certGen.setNotAfter(new Date(System.currentTimeMillis() + 50000));
        certGen.setSubjectDN(new X500Principal("CN=Test Certificate"));
        certGen.setPublicKey(pair.getPublic());
       
// i get error here
 certGen.setSignatureAlgorithm("SHA256WithRSAEncryption");

        return certGen.generateX509Certificate(pair.getPrivate(), "BC");
    }
    
    public static void main(
        String[]    args)
        throws Exception
    {
        // create the keys
        KeyPair          pair = Utils.generateRSAKeyPair();
        
        // generate the certificate
        X509Certificate cert = generateV1Certificate(pair);

        // show some basic validation
        cert.checkValidity(new Date());

        cert.verify(cert.getPublicKey());
        
        System.out.println("valid certificate generated");
    }
}

pls helpppppppppp meeeeeeeeee//

Last edited by Nico; 10-30-06 at 11:10 AM. Reason: Please use [code] wrappers when posting code.
Reply With Quote