KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > proactive > ext > security > crypto > CertificationAuthority


1 /*
2 * ################################################################
3 *
4 * ProActive: The Java(TM) library for Parallel, Distributed,
5 * Concurrent computing with Security and Mobility
6 *
7 * Copyright (C) 1997-2002 INRIA/University of Nice-Sophia Antipolis
8 * Contact: proactive-support@inria.fr
9 *
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or any later version.
14 *
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * USA
24 *
25 * Initial developer(s): The ProActive Team
26 * http://www.inria.fr/oasis/ProActive/contacts.html
27 * Contributor(s):
28 *
29 * ################################################################
30 */

31 package org.objectweb.proactive.ext.security.crypto;
32
33 import org.bouncycastle.jce.provider.JDKKeyPairGenerator;
34
35 import java.io.FileOutputStream JavaDoc;
36 import java.io.ObjectOutputStream JavaDoc;
37
38 import java.security.KeyPair JavaDoc;
39 import java.security.PrivateKey JavaDoc;
40 import java.security.PublicKey JavaDoc;
41 import java.security.SecureRandom JavaDoc;
42
43
44 public class CertificationAuthority {
45     private static PrivateKey JavaDoc privateKey;
46     private static PublicKey JavaDoc publicKey;
47
48     public CertificationAuthority() {
49         generateKeyPair();
50     }
51
52     public static PublicKey JavaDoc get_PublicKey() {
53         return publicKey;
54     }
55
56     public static PrivateKey JavaDoc get_PrivateKey() {
57         return privateKey;
58     }
59
60     public static void writeKeys() {
61         try {
62             System.out.println("Generating AC publicKey...");
63
64             FileOutputStream JavaDoc fout = new FileOutputStream JavaDoc("acPublicKey");
65             ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(fout);
66             out.writeObject(publicKey);
67             out.flush();
68             out.close();
69             System.out.println("Generating AC privateKey...");
70             fout = new FileOutputStream JavaDoc("acPrivateKey");
71             out = new ObjectOutputStream JavaDoc(fout);
72             out.writeObject(privateKey);
73             out.flush();
74             out.close();
75             System.out.println("The KeyPair has been correctly generated.");
76             System.out.println("The AC publicKey is saved in : acPublicKey");
77             System.out.println("The AC privateKey is saved in : acPrivateKey");
78         } catch (Exception JavaDoc e) {
79             System.out.println("Exception in AC key serialization :" + e);
80         }
81     }
82
83     public static void main(String JavaDoc[] args) {
84         new CertificationAuthority();
85         writeKeys();
86     }
87
88     private static void generateKeyPair() {
89         // Provider myProvider = new org.bouncycastle.jce.provider.BouncyCastleProvider();
90
// Tester ici si ca n'a pas ete deja fait : cf mail...
91
// Security.addProvider(myProvider);
92
// Key Pair Generation...
93
SecureRandom JavaDoc rand = new SecureRandom JavaDoc();
94         JDKKeyPairGenerator.RSA keyPairGen = new JDKKeyPairGenerator.RSA();
95         keyPairGen.initialize(512, rand);
96
97         KeyPair JavaDoc keyPair = keyPairGen.generateKeyPair();
98         privateKey = keyPair.getPrivate();
99         publicKey = keyPair.getPublic();
100     }
101 }
102
Popular Tags