Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members

JCODHKeyPair.java

00001 package edu.virtualschool.jco.unfinished;
00002 
00003 import java.math.BigInteger;
00004 import java.security.SecureRandom;
00005 
00006 import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
00007 import org.bouncycastle.crypto.generators.DHKeyPairGenerator;
00008 import org.bouncycastle.crypto.generators.DHParametersGenerator;
00009 import org.bouncycastle.crypto.params.DHKeyGenerationParameters;
00010 import org.bouncycastle.crypto.params.DHParameters;
00011 import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
00012 import org.bouncycastle.crypto.params.DHPublicKeyParameters;
00013 
00021 public final class JCODHKeyPair
00022 {
00023   final JCODHPublicKey publicKey;
00024   final JCODHPrivateKey privateKey;
00025 
00026   final static int DEFAULT_STRENGTH = 1024;
00027   final static int DEFAULT_CERTAINTY = 20;
00028   
00029   public JCODHKeyPair()
00030   {
00031     this(DEFAULT_STRENGTH, DEFAULT_CERTAINTY);
00032   }
00033   public JCODHKeyPair(int strength)
00034   {
00035     this(strength, DEFAULT_CERTAINTY);
00036   }
00037   public JCODHKeyPair(int strength, int certainty)
00038   {
00039     SecureRandom r = new SecureRandom();
00040 //    DHBasicKeyPairGenerator engine = new DHBasicKeyPairGenerator();
00041     DHKeyPairGenerator engine = new DHKeyPairGenerator();
00042     
00043     DHParametersGenerator pGen = new DHParametersGenerator();
00044     pGen.init(strength, certainty, r);
00045     long t0 = System.currentTimeMillis();
00046     DHParameters p = pGen.generateParameters();
00047     long t1 = System.currentTimeMillis();
00048     
00049     System.err.println("aDHParametersGenerator.generateParameters took " + (t1-t0)+" msec");
00050     DHKeyGenerationParameters param = new DHKeyGenerationParameters(r, p);
00051     engine.init(param);
00052 
00053     AsymmetricCipherKeyPair   pair = engine.generateKeyPair();
00054     this.publicKey = new JCODHPublicKey((DHPublicKeyParameters)pair.getPublic());
00055     this.privateKey = new JCODHPrivateKey((DHPrivateKeyParameters)pair.getPrivate());
00056   }
00057   public JCODHKeyPair(BigInteger p, BigInteger g)
00058   {
00059     SecureRandom sr = new SecureRandom();
00060     DHParameters pg = new DHParameters(p, g);
00061     DHKeyGenerationParameters params = new DHKeyGenerationParameters(sr, pg);
00062     DHKeyPairGenerator gen = new DHKeyPairGenerator();
00063     gen.init(params);
00064       
00065     AsymmetricCipherKeyPair pair = gen.generateKeyPair();
00066     this.publicKey = new JCODHPublicKey((DHPublicKeyParameters)pair.getPublic());
00067     this.privateKey = new JCODHPrivateKey((DHPrivateKeyParameters)pair.getPrivate());
00068   }
00069   public final JCODHPublicKey getPublic()
00070   {
00071     return publicKey;
00072   }
00073   public final JCODHPrivateKey getPrivate()
00074   {
00075     return privateKey;
00076   }
00077 }