org/objectweb/proactive/ext/security/crypto/CertificationAuthority.java

00001 /* 
00002  * ################################################################
00003  * 
00004  * ProActive: The Java(TM) library for Parallel, Distributed, 
00005  *            Concurrent computing with Security and Mobility
00006  * 
00007  * Copyright (C) 1997-2007 INRIA/University of Nice-Sophia Antipolis
00008  * Contact: proactive@objectweb.org
00009  * 
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or any later version.
00014  *  
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  * 
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
00023  * USA
00024  *  
00025  *  Initial developer(s):               The ProActive Team
00026  *                        http://www.inria.fr/oasis/ProActive/contacts.html
00027  *  Contributor(s): 
00028  * 
00029  * ################################################################
00030  */ 
00031 package org.objectweb.proactive.ext.security.crypto;
00032 
00033 import java.io.FileOutputStream;
00034 import java.io.ObjectOutputStream;
00035 import java.security.KeyPair;
00036 import java.security.PrivateKey;
00037 import java.security.PublicKey;
00038 import java.security.SecureRandom;
00039 
00040 import org.bouncycastle.jce.provider.JDKKeyPairGenerator;
00041 
00042 
00043 public class CertificationAuthority {
00044     private static PrivateKey privateKey;
00045     private static PublicKey publicKey;
00046 
00047     public CertificationAuthority() {
00048         generateKeyPair();
00049     }
00050 
00051     public static PublicKey get_PublicKey() {
00052         return publicKey;
00053     }
00054 
00055     public static PrivateKey get_PrivateKey() {
00056         return privateKey;
00057     }
00058 
00059     public static void writeKeys() {
00060         try {
00061             System.out.println("Generating AC publicKey...");
00062 
00063             FileOutputStream fout = new FileOutputStream("acPublicKey");
00064             ObjectOutputStream out = new ObjectOutputStream(fout);
00065             out.writeObject(publicKey);
00066             out.flush();
00067             out.close();
00068             System.out.println("Generating AC privateKey...");
00069             fout = new FileOutputStream("acPrivateKey");
00070             out = new ObjectOutputStream(fout);
00071             out.writeObject(privateKey);
00072             out.flush();
00073             out.close();
00074             System.out.println("The KeyPair has been correctly generated.");
00075             System.out.println("The AC publicKey  is saved in : acPublicKey");
00076             System.out.println("The AC privateKey is saved in : acPrivateKey");
00077         } catch (Exception e) {
00078             System.out.println("Exception in AC key serialization :" + e);
00079         }
00080     }
00081 
00082     public static void main(String[] args) {
00083         new CertificationAuthority();
00084         writeKeys();
00085     }
00086 
00087     private static void generateKeyPair() {
00088         //   Provider myProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
00089         // Tester ici si ca n'a pas ete deja fait : cf mail...
00090         //  Security.addProvider(myProvider);
00091         // Key Pair Generation...
00092         SecureRandom rand = new SecureRandom();
00093         JDKKeyPairGenerator.RSA keyPairGen = new JDKKeyPairGenerator.RSA();
00094         keyPairGen.initialize(512, rand);
00095 
00096         KeyPair keyPair = keyPairGen.generateKeyPair();
00097         privateKey = keyPair.getPrivate();
00098         publicKey = keyPair.getPublic();
00099     }
00100 }

Generated on Mon Jan 22 15:16:10 2007 for ProActive by  doxygen 1.5.1