01: package org.bouncycastle.jce.provider;
02:
03: import java.security.InvalidKeyException;
04: import java.security.PrivateKey;
05: import java.security.PublicKey;
06:
07: import javax.crypto.interfaces.DHPrivateKey;
08: import javax.crypto.interfaces.DHPublicKey;
09:
10: import org.bouncycastle.crypto.params.AsymmetricKeyParameter;
11: import org.bouncycastle.crypto.params.ElGamalParameters;
12: import org.bouncycastle.crypto.params.ElGamalPrivateKeyParameters;
13: import org.bouncycastle.crypto.params.ElGamalPublicKeyParameters;
14: import org.bouncycastle.jce.interfaces.ElGamalPrivateKey;
15: import org.bouncycastle.jce.interfaces.ElGamalPublicKey;
16:
17: /**
18: * utility class for converting jce/jca ElGamal objects
19: * objects into their org.bouncycastle.crypto counterparts.
20: */
21: public class ElGamalUtil {
22: static public AsymmetricKeyParameter generatePublicKeyParameter(
23: PublicKey key) throws InvalidKeyException {
24: if (key instanceof ElGamalPublicKey) {
25: ElGamalPublicKey k = (ElGamalPublicKey) key;
26:
27: return new ElGamalPublicKeyParameters(k.getY(),
28: new ElGamalParameters(k.getParameters().getP(), k
29: .getParameters().getG()));
30: } else if (key instanceof DHPublicKey) {
31: DHPublicKey k = (DHPublicKey) key;
32:
33: return new ElGamalPublicKeyParameters(k.getY(),
34: new ElGamalParameters(k.getParams().getP(), k
35: .getParams().getG()));
36: }
37:
38: throw new InvalidKeyException(
39: "can't identify public key for El Gamal.");
40: }
41:
42: static public AsymmetricKeyParameter generatePrivateKeyParameter(
43: PrivateKey key) throws InvalidKeyException {
44: if (key instanceof ElGamalPrivateKey) {
45: ElGamalPrivateKey k = (ElGamalPrivateKey) key;
46:
47: return new ElGamalPrivateKeyParameters(k.getX(),
48: new ElGamalParameters(k.getParameters().getP(), k
49: .getParameters().getG()));
50: } else if (key instanceof DHPrivateKey) {
51: DHPrivateKey k = (DHPrivateKey) key;
52:
53: return new ElGamalPrivateKeyParameters(k.getX(),
54: new ElGamalParameters(k.getParams().getP(), k
55: .getParams().getG()));
56: }
57:
58: throw new InvalidKeyException(
59: "can't identify private key for El Gamal.");
60: }
61: }
|