Search in sources :

Example 1 with DHPrivateKeySpec

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;
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) PrivateKey(java.security.PrivateKey) DHPrivateKeySpec(javax.crypto.spec.DHPrivateKeySpec) BigInteger(java.math.BigInteger) DHParameterSpec(javax.crypto.spec.DHParameterSpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 2 with DHPrivateKeySpec

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);
    }
}
Also used : DHPrivateKey(javax.crypto.interfaces.DHPrivateKey) KeyPair(java.security.KeyPair) AsymmetricCipherKeyPair(org.spongycastle.crypto.AsymmetricCipherKeyPair) DHPublicKeyParameters(org.spongycastle.crypto.params.DHPublicKeyParameters) DHParameters(org.spongycastle.crypto.params.DHParameters) DHPublicKey(javax.crypto.interfaces.DHPublicKey) DHPrivateKeyParameters(org.spongycastle.crypto.params.DHPrivateKeyParameters) DHKeyGenerationParameters(org.spongycastle.crypto.params.DHKeyGenerationParameters) SecureRandom(java.security.SecureRandom) DHKeyPairGenerator(org.spongycastle.crypto.generators.DHKeyPairGenerator) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) AsymmetricCipherKeyPair(org.spongycastle.crypto.AsymmetricCipherKeyPair) DHPrivateKeySpec(javax.crypto.spec.DHPrivateKeySpec) DHPublicKeySpec(javax.crypto.spec.DHPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 3 with DHPrivateKeySpec

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]);
    }
}
Also used : DHPrivateKeySpec(javax.crypto.spec.DHPrivateKeySpec) BigInteger(java.math.BigInteger)

Example 4 with DHPrivateKeySpec

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");
    }
}
Also used : DHParameterSpec(javax.crypto.spec.DHParameterSpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DHPrivateKeySpec(javax.crypto.spec.DHPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) DHPublicKeySpec(javax.crypto.spec.DHPublicKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 5 with DHPrivateKeySpec

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");
    }
}
Also used : DHParameterSpec(javax.crypto.spec.DHParameterSpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DHPrivateKeySpec(javax.crypto.spec.DHPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) DHPublicKeySpec(javax.crypto.spec.DHPublicKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Aggregations

DHPrivateKeySpec (javax.crypto.spec.DHPrivateKeySpec)6 BigInteger (java.math.BigInteger)3 DHParameterSpec (javax.crypto.spec.DHParameterSpec)3 DHPublicKeySpec (javax.crypto.spec.DHPublicKeySpec)3 KeyFactory (java.security.KeyFactory)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)2 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)2 DHPrivateKey (javax.crypto.interfaces.DHPrivateKey)2 IOException (java.io.IOException)1 InvalidKeyException (java.security.InvalidKeyException)1 KeyPair (java.security.KeyPair)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 PrivateKey (java.security.PrivateKey)1 SecureRandom (java.security.SecureRandom)1 SecretKeyFactory (javax.crypto.SecretKeyFactory)1 DHPublicKey (javax.crypto.interfaces.DHPublicKey)1 AsymmetricCipherKeyPair (org.spongycastle.crypto.AsymmetricCipherKeyPair)1 DHKeyPairGenerator (org.spongycastle.crypto.generators.DHKeyPairGenerator)1 DHKeyGenerationParameters (org.spongycastle.crypto.params.DHKeyGenerationParameters)1