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

JCOKeyPair.java

00001 package edu.virtualschool.jco;
00002 
00003 import java.math.BigInteger;
00004 import java.security.SecureRandom;
00005 
00006 import org.bouncycastle.crypto.AsymmetricCipherKeyPair;
00007 import org.bouncycastle.crypto.generators.RSAKeyPairGenerator;
00008 import org.bouncycastle.crypto.params.RSAKeyGenerationParameters;
00009 import org.bouncycastle.crypto.params.RSAKeyParameters;
00010 import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
00011 
00018 public final class JCOKeyPair
00019 {
00020   final JCOPublicKey publicKey;
00021   final JCOPrivateKey privateKey;
00022 
00023   static final int DEFAULT_KEY_LENGTH = 2048/8; // bytes,
00024   static final int DEFAULT_KEY_CERTAINTY = 25;
00025   static final BigInteger publicExponent = BigInteger.valueOf(0x11);
00026   
00027   public JCOKeyPair(JCOPublicKey pub, JCOPrivateKey prv)
00028   {
00029     this.privateKey = prv;
00030     this.publicKey = pub;
00031   }
00035   public JCOKeyPair()
00036   {
00037     this(DEFAULT_KEY_LENGTH, DEFAULT_KEY_CERTAINTY);
00038   }
00045   public JCOKeyPair(int length)
00046   {
00047     this(length, DEFAULT_KEY_CERTAINTY);
00048   }
00049   JCOKeyPair(int keyLengthInBytes, int certainty)
00050   {
00051     SecureRandom random = new SecureRandom();
00052     RSAKeyGenerationParameters  kgp = new RSAKeyGenerationParameters
00053       (publicExponent, random, keyLengthInBytes*8, certainty);
00054       
00055     RSAKeyPairGenerator kpg = new RSAKeyPairGenerator();
00056     kpg.init(kgp);
00057     AsymmetricCipherKeyPair pair = kpg.generateKeyPair();
00058     
00059     this.privateKey  = new JCOPrivateKey((RSAPrivateCrtKeyParameters)pair.getPrivate());
00060     this.publicKey = new JCOPublicKey((RSAKeyParameters)pair.getPublic());
00061   }
00062   public final JCOPublicKey getPublic()
00063   {
00064     return publicKey;
00065   }
00066   public final JCOPrivateKey getPrivate()
00067   {
00068     return privateKey;
00069   }
00070 }