1 31 package org.objectweb.proactive.ext.security.crypto; 32 33 import org.bouncycastle.jce.provider.JDKKeyPairGenerator; 34 35 import java.io.FileOutputStream ; 36 import java.io.ObjectOutputStream ; 37 38 import java.security.KeyPair ; 39 import java.security.PrivateKey ; 40 import java.security.PublicKey ; 41 import java.security.SecureRandom ; 42 43 44 public class CertificationAuthority { 45 private static PrivateKey privateKey; 46 private static PublicKey publicKey; 47 48 public CertificationAuthority() { 49 generateKeyPair(); 50 } 51 52 public static PublicKey get_PublicKey() { 53 return publicKey; 54 } 55 56 public static PrivateKey get_PrivateKey() { 57 return privateKey; 58 } 59 60 public static void writeKeys() { 61 try { 62 System.out.println("Generating AC publicKey..."); 63 64 FileOutputStream fout = new FileOutputStream ("acPublicKey"); 65 ObjectOutputStream out = new ObjectOutputStream (fout); 66 out.writeObject(publicKey); 67 out.flush(); 68 out.close(); 69 System.out.println("Generating AC privateKey..."); 70 fout = new FileOutputStream ("acPrivateKey"); 71 out = new ObjectOutputStream (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 e) { 79 System.out.println("Exception in AC key serialization :" + e); 80 } 81 } 82 83 public static void main(String [] args) { 84 new CertificationAuthority(); 85 writeKeys(); 86 } 87 88 private static void generateKeyPair() { 89 SecureRandom rand = new SecureRandom (); 94 JDKKeyPairGenerator.RSA keyPairGen = new JDKKeyPairGenerator.RSA(); 95 keyPairGen.initialize(512, rand); 96 97 KeyPair keyPair = keyPairGen.generateKeyPair(); 98 privateKey = keyPair.getPrivate(); 99 publicKey = keyPair.getPublic(); 100 } 101 } 102 | Popular Tags |