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.DHParameters;
12: import org.bouncycastle.crypto.params.DHPrivateKeyParameters;
13: import org.bouncycastle.crypto.params.DHPublicKeyParameters;
14:
15: /**
16: * utility class for converting jce/jca DH objects
17: * objects into their org.bouncycastle.crypto counterparts.
18: */
19: public class DHUtil {
20: static public AsymmetricKeyParameter generatePublicKeyParameter(
21: PublicKey key) throws InvalidKeyException {
22: if (key instanceof DHPublicKey) {
23: DHPublicKey k = (DHPublicKey) key;
24:
25: return new DHPublicKeyParameters(k.getY(),
26: new DHParameters(k.getParams().getP(), k
27: .getParams().getG(), null, k.getParams()
28: .getL()));
29: }
30:
31: throw new InvalidKeyException("can't identify DH public key.");
32: }
33:
34: static public AsymmetricKeyParameter generatePrivateKeyParameter(
35: PrivateKey key) throws InvalidKeyException {
36: if (key instanceof DHPrivateKey) {
37: DHPrivateKey k = (DHPrivateKey) key;
38:
39: return new DHPrivateKeyParameters(k.getX(),
40: new DHParameters(k.getParams().getP(), k
41: .getParams().getG(), null, k.getParams()
42: .getL()));
43: }
44:
45: throw new InvalidKeyException("can't identify DH private key.");
46: }
47: }
|