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

JCODHPrivateKey.java

00001 package edu.virtualschool.jco.unfinished;
00002 
00003 import java.io.ByteArrayOutputStream;
00004 import java.io.IOException;
00005 import java.io.Serializable;
00006 import java.math.BigInteger;
00007 
00008 import org.bouncycastle.asn1.DERInteger;
00009 import org.bouncycastle.asn1.DEROutputStream;
00010 import org.bouncycastle.asn1.pkcs.DHParameter;
00011 import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
00012 import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
00013 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
00014 import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
00015 import org.bouncycastle.crypto.params.DHParameters;
00016 import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
00017 
00018 import edu.virtualschool.jco.JCOAsymmetricKey;
00019 import edu.virtualschool.jco.JCOEncodedBytes;
00020 import edu.virtualschool.jco.JCOFault;
00021 import edu.virtualschool.jco.JCOSignedBytes;
00022 import edu.virtualschool.jwaa.RuntimeFault;
00023 
00033 public final class JCODHPrivateKey extends JCOAsymmetricKey
00034 {
00035   private final BigInteger x;
00036 //  private final DHParameterSpec dhSpec;
00037   private final DHParameters params;
00038   
00039   JCODHPrivateKey(DHPrivateKeyParameters key)
00040   {
00041       this.x = key.getX();
00042       this.params = key.getParameters();
00043 //      this.dhSpec = new DHParameterSpec(p.getP(), p.getG());
00044   }
00045 /*
00046   private JCODHPrivateKey(PKCS8EncodedKeySpec spec) throws JCOFault
00047   {
00048     try
00049     {
00050       byte[] bytes = spec.getEncoded();
00051       ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
00052       DERInputStream dis = new DERInputStream(bis);
00053       Object obj = dis.readObject();
00054       PrivateKeyInfo info = new PrivateKeyInfo((ASN1Sequence)obj);
00055       ASN1Sequence asn = (ASN1Sequence)info.getAlgorithmId().getParameters();
00056       DHParameter params = new DHParameter(asn);
00057       DERInteger derX = (DERInteger)info.getPrivateKey();
00058 
00059       this.x = derX.getValue();
00060       this.params = new DHParameters(params.getP(), params.getG());
00061 
00062 //      if (params.getL() != null)
00063 //          this.dhSpec = new DHParameterSpec(params.getP(), params.getG(), params.getL().intValue());
00064 //      else
00065 //          this.dhSpec = new DHParameterSpec(params.getP(), params.getG());
00066     }
00067     catch (Exception e)
00068     {
00069         throw new JCOFault(e, e);
00070     }
00071   }
00072 
00073   private JCODHPrivateKey(DHPrivateKeySpec spec)
00074    {
00075       this.x = spec.getX();
00076       this.params = new DHParameters(spec.getP(), spec.getG());
00077    }
00078 */
00079   public JCOEncodedBytes encode()
00080   {
00081     return new JCOEncodedBytes(getEncoded()); 
00082   }
00083   public JCOSignedBytes sign(Serializable object)
00084     throws JCOFault
00085   {
00086     return new JCOSignedBytes(object, this);
00087   }
00088   //
00089   // PKCS1 - private encrypt, public decrypt
00090   //
00091   public byte[] encryptBytes(byte[] inputBytes)
00092     throws JCOFault
00093   {
00094     throw new JCOFault("DH encryptBytes no implemented");
00095 //    AsymmetricBlockCipher cipher = new RSAEngine();
00096 //    PKCS1Encoding engine = new PKCS1Encoding
00097 //      (((PKCS1Encoding)cipher).getUnderlyingCipher());
00098 //    AsymmetricKeyParameter parms = getParameters();
00099 //    engine.init(true, parms);
00100 //    try
00101 //    {
00102 //      return engine.processBlock(inputBytes, 0, inputBytes.length);
00103 //    }
00104 //    catch (Throwable e) { throw new JCOFault(e); }
00105   }
00106   //
00107   // OAEP - public encrypt, private decrypt
00108   //
00109   public byte[] decryptBytes(byte[] inputBytes)
00110     throws JCOFault
00111   {
00112     throw new JCOFault("DH decryptBytes no implemented");
00113 //    AsymmetricBlockCipher cipher = new RSAEngine();
00114 //    OAEPEncoding engine = new OAEPEncoding(((PKCS1Encoding)cipher).getUnderlyingCipher());
00115 //    AsymmetricKeyParameter parms = getParameters();
00116 //    engine.init(false, parms);
00117 //    try
00118 //    {
00119 //      return engine.processBlock(inputBytes, 0, inputBytes.length);
00120 //    }
00121 //    catch (Throwable e) { throw new JCOFault(e); }
00122   }
00123   public DHParameters getParams()
00124   {
00125      return params;
00126   }
00127   public BigInteger getX()
00128   {
00129      return x;
00130   }
00131   public final String getAlgorithm() 
00132   {
00133     return "DH"; 
00134   }
00135   public String getFormat()
00136   {
00137       return "PKCS#8";
00138   }
00139   public byte[] getEncoded()
00140   {
00141       ByteArrayOutputStream   bOut = new ByteArrayOutputStream();
00142       DEROutputStream dOut = new DEROutputStream(bOut);
00143       DHParameter dhp = new DHParameter(params.getP(), params.getG(), 0);
00144       AlgorithmIdentifier aid = new AlgorithmIdentifier(PKCSObjectIdentifiers.dhKeyAgreement, dhp.getDERObject());
00145       PrivateKeyInfo info = new PrivateKeyInfo(aid, new DERInteger(getX()));
00146       try
00147       {
00148         dOut.writeObject(info);
00149         dOut.close();
00150         return bOut.toByteArray();
00151       }
00152       catch (IOException e)
00153       {
00154           throw new RuntimeFault(e, e);
00155       }
00156   }
00157   public final AsymmetricKeyParameter getParameters()
00158   {
00159 //    DHParameters params = new DHParameters(params.getP(), params.getG());
00160     return new DHPrivateKeyParameters(x, params);
00161   }
00162 }