use of javax.crypto.spec.DHPrivateKeySpec in project ofbiz-framework by apache.
the class ValueLinkApi method getPrivateKey.
/**
* Get merchant Private Key
* @return PrivateKey object for the merchant
*/
public PrivateKey getPrivateKey() throws InvalidKeySpecException, NoSuchAlgorithmException {
byte[] privateKeyBytes = this.getPrivateKeyBytes();
// initialize the parameter spec
DHParameterSpec dhParamSpec = this.getDHParameterSpec();
// load the private key
KeyFactory keyFactory = KeyFactory.getInstance("DH");
BigInteger privateKeyInt = new BigInteger(privateKeyBytes);
DHPrivateKeySpec dhPrivateSpec = new DHPrivateKeySpec(privateKeyInt, dhParamSpec.getP(), dhParamSpec.getG());
PrivateKey privateKey = keyFactory.generatePrivate(dhPrivateSpec);
return privateKey;
}
use of javax.crypto.spec.DHPrivateKeySpec in project Zom-Android by zom.
the class OtrCryptoEngineImpl method generateDHKeyPair.
public KeyPair generateDHKeyPair() throws OtrCryptoException {
// Generate a AsymmetricCipherKeyPair using BC.
DHParameters dhParams = new DHParameters(MODULUS, GENERATOR, null, DH_PRIVATE_KEY_MINIMUM_BIT_LENGTH);
DHKeyGenerationParameters params = new DHKeyGenerationParameters(new SecureRandom(), dhParams);
DHKeyPairGenerator kpGen = new DHKeyPairGenerator();
kpGen.init(params);
AsymmetricCipherKeyPair pair = kpGen.generateKeyPair();
// Convert this AsymmetricCipherKeyPair to a standard JCE KeyPair.
DHPublicKeyParameters pub = (DHPublicKeyParameters) pair.getPublic();
DHPrivateKeyParameters priv = (DHPrivateKeyParameters) pair.getPrivate();
try {
KeyFactory keyFac = KeyFactory.getInstance("DH");
DHPublicKeySpec pubKeySpecs = new DHPublicKeySpec(pub.getY(), MODULUS, GENERATOR);
DHPublicKey pubKey = (DHPublicKey) keyFac.generatePublic(pubKeySpecs);
DHParameters dhParameters = priv.getParameters();
DHPrivateKeySpec privKeySpecs = new DHPrivateKeySpec(priv.getX(), dhParameters.getP(), dhParameters.getG());
DHPrivateKey privKey = (DHPrivateKey) keyFac.generatePrivate(privKeySpecs);
return new KeyPair(pubKey, privKey);
} catch (Exception e) {
throw new OtrCryptoException(e);
}
}
use of javax.crypto.spec.DHPrivateKeySpec in project robovm by robovm.
the class DHPrivateKeySpecTest method testDHPrivateKeySpec.
/**
* DHPrivateKeySpec class testing. Tests the equivalence of parameters
* specified in the constructor with the values returned by getters.
*/
public void testDHPrivateKeySpec() {
BigInteger[] xs = { new BigInteger("-1000000000000"), BigInteger.ZERO, BigInteger.ONE, new BigInteger("1000000000000") };
BigInteger[] ps = { new BigInteger("-1000000000000"), BigInteger.ZERO, BigInteger.ONE, new BigInteger("1000000000000") };
BigInteger[] gs = { new BigInteger("-1000000000000"), BigInteger.ZERO, BigInteger.ONE, new BigInteger("1000000000000") };
for (int i = 0; i < ps.length; i++) {
DHPrivateKeySpec dhpks = new DHPrivateKeySpec(xs[i], ps[i], gs[i]);
assertEquals("The value returned by getX() must be " + "equal to the value specified in the constructor", dhpks.getX(), xs[i]);
assertEquals("The value returned by getP() must be " + "equal to the value specified in the constructor", dhpks.getP(), ps[i]);
assertEquals("The value returned by getG() must be " + "equal to the value specified in the constructor", dhpks.getG(), gs[i]);
}
}
use of javax.crypto.spec.DHPrivateKeySpec in project jdk8u_jdk by JetBrains.
the class DHKeyFactory method engineGetKeySpec.
/**
* Returns a specification (key material) of the given key object
* in the requested format.
*
* @param key the key
*
* @param keySpec the requested format in which the key material shall be
* returned
*
* @return the underlying key specification (key material) in the
* requested format
*
* @exception InvalidKeySpecException if the requested key specification is
* inappropriate for the given key, or the given key cannot be processed
* (e.g., the given key has an unrecognized algorithm or format).
*/
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
DHParameterSpec params;
if (key instanceof javax.crypto.interfaces.DHPublicKey) {
if (DHPublicKeySpec.class.isAssignableFrom(keySpec)) {
javax.crypto.interfaces.DHPublicKey dhPubKey = (javax.crypto.interfaces.DHPublicKey) key;
params = dhPubKey.getParams();
return keySpec.cast(new DHPublicKeySpec(dhPubKey.getY(), params.getP(), params.getG()));
} else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
} else {
throw new InvalidKeySpecException("Inappropriate key specification");
}
} else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
if (DHPrivateKeySpec.class.isAssignableFrom(keySpec)) {
javax.crypto.interfaces.DHPrivateKey dhPrivKey = (javax.crypto.interfaces.DHPrivateKey) key;
params = dhPrivKey.getParams();
return keySpec.cast(new DHPrivateKeySpec(dhPrivKey.getX(), params.getP(), params.getG()));
} else if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
} else {
throw new InvalidKeySpecException("Inappropriate key specification");
}
} else {
throw new InvalidKeySpecException("Inappropriate key type");
}
}
use of javax.crypto.spec.DHPrivateKeySpec in project Bytecoder by mirkosertic.
the class DHKeyFactory method engineGetKeySpec.
/**
* Returns a specification (key material) of the given key object
* in the requested format.
*
* @param key the key
*
* @param keySpec the requested format in which the key material shall be
* returned
*
* @return the underlying key specification (key material) in the
* requested format
*
* @exception InvalidKeySpecException if the requested key specification is
* inappropriate for the given key, or the given key cannot be processed
* (e.g., the given key has an unrecognized algorithm or format).
*/
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
DHParameterSpec params;
if (key instanceof javax.crypto.interfaces.DHPublicKey) {
if (DHPublicKeySpec.class.isAssignableFrom(keySpec)) {
javax.crypto.interfaces.DHPublicKey dhPubKey = (javax.crypto.interfaces.DHPublicKey) key;
params = dhPubKey.getParams();
return keySpec.cast(new DHPublicKeySpec(dhPubKey.getY(), params.getP(), params.getG()));
} else if (X509EncodedKeySpec.class.isAssignableFrom(keySpec)) {
return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
} else {
throw new InvalidKeySpecException("Inappropriate key specification");
}
} else if (key instanceof javax.crypto.interfaces.DHPrivateKey) {
if (DHPrivateKeySpec.class.isAssignableFrom(keySpec)) {
javax.crypto.interfaces.DHPrivateKey dhPrivKey = (javax.crypto.interfaces.DHPrivateKey) key;
params = dhPrivKey.getParams();
return keySpec.cast(new DHPrivateKeySpec(dhPrivKey.getX(), params.getP(), params.getG()));
} else if (PKCS8EncodedKeySpec.class.isAssignableFrom(keySpec)) {
return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
} else {
throw new InvalidKeySpecException("Inappropriate key specification");
}
} else {
throw new InvalidKeySpecException("Inappropriate key type");
}
}
Aggregations