001: package org.bouncycastle.jce.provider;
002:
003: import java.io.IOException;
004: import java.io.ObjectInputStream;
005: import java.io.ObjectOutputStream;
006: import java.math.BigInteger;
007:
008: import javax.crypto.interfaces.DHPublicKey;
009: import javax.crypto.spec.DHParameterSpec;
010: import javax.crypto.spec.DHPublicKeySpec;
011:
012: import org.bouncycastle.asn1.ASN1Sequence;
013: import org.bouncycastle.asn1.DERInteger;
014: import org.bouncycastle.asn1.oiw.ElGamalParameter;
015: import org.bouncycastle.asn1.oiw.OIWObjectIdentifiers;
016: import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
017: import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
018: import org.bouncycastle.crypto.params.ElGamalPublicKeyParameters;
019: import org.bouncycastle.jce.interfaces.ElGamalPublicKey;
020: import org.bouncycastle.jce.spec.ElGamalParameterSpec;
021: import org.bouncycastle.jce.spec.ElGamalPublicKeySpec;
022:
023: public class JCEElGamalPublicKey implements ElGamalPublicKey,
024: DHPublicKey {
025: static final long serialVersionUID = 8712728417091216948L;
026:
027: private BigInteger y;
028: private ElGamalParameterSpec elSpec;
029:
030: JCEElGamalPublicKey(ElGamalPublicKeySpec spec) {
031: this .y = spec.getY();
032: this .elSpec = new ElGamalParameterSpec(spec.getParams().getP(),
033: spec.getParams().getG());
034: }
035:
036: JCEElGamalPublicKey(DHPublicKeySpec spec) {
037: this .y = spec.getY();
038: this .elSpec = new ElGamalParameterSpec(spec.getP(), spec.getG());
039: }
040:
041: JCEElGamalPublicKey(ElGamalPublicKey key) {
042: this .y = key.getY();
043: this .elSpec = key.getParameters();
044: }
045:
046: JCEElGamalPublicKey(DHPublicKey key) {
047: this .y = key.getY();
048: this .elSpec = new ElGamalParameterSpec(key.getParams().getP(),
049: key.getParams().getG());
050: }
051:
052: JCEElGamalPublicKey(ElGamalPublicKeyParameters params) {
053: this .y = params.getY();
054: this .elSpec = new ElGamalParameterSpec(params.getParameters()
055: .getP(), params.getParameters().getG());
056: }
057:
058: JCEElGamalPublicKey(BigInteger y, ElGamalParameterSpec elSpec) {
059: this .y = y;
060: this .elSpec = elSpec;
061: }
062:
063: JCEElGamalPublicKey(SubjectPublicKeyInfo info) {
064: ElGamalParameter params = new ElGamalParameter(
065: (ASN1Sequence) info.getAlgorithmId().getParameters());
066: DERInteger derY = null;
067:
068: try {
069: derY = (DERInteger) info.getPublicKey();
070: } catch (IOException e) {
071: throw new IllegalArgumentException(
072: "invalid info structure in DSA public key");
073: }
074:
075: this .y = derY.getValue();
076: this .elSpec = new ElGamalParameterSpec(params.getP(), params
077: .getG());
078: }
079:
080: public String getAlgorithm() {
081: return "ElGamal";
082: }
083:
084: public String getFormat() {
085: return "X.509";
086: }
087:
088: public byte[] getEncoded() {
089: SubjectPublicKeyInfo info = new SubjectPublicKeyInfo(
090: new AlgorithmIdentifier(
091: OIWObjectIdentifiers.elGamalAlgorithm,
092: new ElGamalParameter(elSpec.getP(), elSpec
093: .getG()).getDERObject()),
094: new DERInteger(y));
095:
096: return info.getDEREncoded();
097: }
098:
099: public ElGamalParameterSpec getParameters() {
100: return elSpec;
101: }
102:
103: public DHParameterSpec getParams() {
104: return new DHParameterSpec(elSpec.getP(), elSpec.getG());
105: }
106:
107: public BigInteger getY() {
108: return y;
109: }
110:
111: private void readObject(ObjectInputStream in) throws IOException,
112: ClassNotFoundException {
113: this .y = (BigInteger) in.readObject();
114: this .elSpec = new ElGamalParameterSpec((BigInteger) in
115: .readObject(), (BigInteger) in.readObject());
116: }
117:
118: private void writeObject(ObjectOutputStream out) throws IOException {
119: out.writeObject(this.getY());
120: out.writeObject(elSpec.getP());
121: out.writeObject(elSpec.getG());
122: }
123: }
|