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
00037 private final DHParameters params;
00038
00039 JCODHPrivateKey(DHPrivateKeyParameters key)
00040 {
00041 this.x = key.getX();
00042 this.params = key.getParameters();
00043
00044 }
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
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
00090
00091 public byte[] encryptBytes(byte[] inputBytes)
00092 throws JCOFault
00093 {
00094 throw new JCOFault("DH encryptBytes no implemented");
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105 }
00106
00107
00108
00109 public byte[] decryptBytes(byte[] inputBytes)
00110 throws JCOFault
00111 {
00112 throw new JCOFault("DH decryptBytes no implemented");
00113
00114
00115
00116
00117
00118
00119
00120
00121
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
00160 return new DHPrivateKeyParameters(x, params);
00161 }
00162 }