Search in sources :

Example 31 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project robovm by robovm.

the class DSAKeyFactoryImpl method engineTranslateKey.

/**
     * The method generates a DSAPublicKey object from the provided key.
     *
     * @param
     *    key - a DSAPublicKey object or DSAPrivateKey object.
     *
     * @return
     *    object of the same type as the "key" argument
     *
     * @throws InvalidKeyException
     *     if "key" is neither DSAPublicKey nor DSAPrivateKey
     */
protected Key engineTranslateKey(Key key) throws InvalidKeyException {
    if (key != null) {
        if (key instanceof DSAPrivateKey) {
            DSAPrivateKey privateKey = (DSAPrivateKey) key;
            DSAParams params = privateKey.getParams();
            try {
                return engineGeneratePrivate(new DSAPrivateKeySpec(privateKey.getX(), params.getP(), params.getQ(), params.getG()));
            } catch (InvalidKeySpecException e) {
                // Actually this exception shouldn't be thrown
                throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e);
            }
        }
        if (key instanceof DSAPublicKey) {
            DSAPublicKey publicKey = (DSAPublicKey) key;
            DSAParams params = publicKey.getParams();
            try {
                return engineGeneratePublic(new DSAPublicKeySpec(publicKey.getY(), params.getP(), params.getQ(), params.getG()));
            } catch (InvalidKeySpecException e) {
                // Actually this exception shouldn't be thrown
                throw new InvalidKeyException("ATTENTION: InvalidKeySpecException: " + e);
            }
        }
    }
    throw new InvalidKeyException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) DSAParams(java.security.interfaces.DSAParams) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 32 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project Conversations by siacs.

the class OtrService method loadKey.

private KeyPair loadKey(final JSONObject keys) {
    if (keys == null) {
        return null;
    }
    synchronized (keys) {
        try {
            BigInteger x = new BigInteger(keys.getString("otr_x"), 16);
            BigInteger y = new BigInteger(keys.getString("otr_y"), 16);
            BigInteger p = new BigInteger(keys.getString("otr_p"), 16);
            BigInteger q = new BigInteger(keys.getString("otr_q"), 16);
            BigInteger g = new BigInteger(keys.getString("otr_g"), 16);
            KeyFactory keyFactory = KeyFactory.getInstance("DSA");
            DSAPublicKeySpec pubKeySpec = new DSAPublicKeySpec(y, p, q, g);
            DSAPrivateKeySpec privateKeySpec = new DSAPrivateKeySpec(x, p, q, g);
            PublicKey publicKey = keyFactory.generatePublic(pubKeySpec);
            PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec);
            return new KeyPair(publicKey, privateKey);
        } catch (JSONException e) {
            return null;
        } catch (NoSuchAlgorithmException e) {
            return null;
        } catch (InvalidKeySpecException e) {
            return null;
        }
    }
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) BigInteger(java.math.BigInteger) JSONException(org.json.JSONException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 33 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project robovm by robovm.

the class KeyFactoryTest method testGeneratePrivate.

@SuppressWarnings("unchecked")
public void testGeneratePrivate() {
    KeyFactory factory = null;
    try {
        factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    }
    assertNotNull(factory);
    try {
        TestPrivateKey key = new TestPrivateKey();
        TestPrivateKeySpec keySpec = new TestPrivateKeySpec(key);
        PrivateKey privateKey = factory.generatePrivate(keySpec);
        assertNotNull(privateKey);
        assertTrue(Arrays.equals(key.getEncoded(), privateKey.getEncoded()));
    } catch (InvalidKeySpecException e) {
        fail("unexpected exception: " + e);
    }
    KeySpec[] keySpecs = { new TestPublicKeySpec(new TestPublicKey()), null, new DSAPublicKeySpec(null, null, null, null) };
    Class[] exceptions = { InvalidKeySpecException.class, NullPointerException.class, InvalidKeySpecException.class };
    for (int i = 0; i < keySpecs.length; i++) {
        KeySpec keySpec = keySpecs[i];
        exceptionThrown = false;
        String message = "generatePrivate(" + (keySpec == null ? "null" : keySpec.toString()) + ")";
        try {
            factory.generatePrivate(keySpec);
        } catch (Exception e) {
            checkException(message, e, exceptions[i]);
        } finally {
            checkException(message, null, exceptions[i]);
        }
    }
}
Also used : PrivateKey(java.security.PrivateKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) KeySpec(java.security.spec.KeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 34 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project robovm by robovm.

the class DSAPublicKeySpecTest method testGetY.

/**
     * Test for <code>getY</code> method
     */
public final void testGetY() {
    DSAPublicKeySpec dpks = new DSAPublicKeySpec(// y
    new BigInteger("1"), // p
    new BigInteger("2"), // q
    new BigInteger("3"), // g
    new BigInteger("4"));
    assertEquals(1, dpks.getY().intValue());
}
Also used : BigInteger(java.math.BigInteger) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 35 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project robovm by robovm.

the class DSAPublicKeySpecTest method testGetG.

/**
     * Test for <code>getG</code> method
     */
public final void testGetG() {
    DSAPublicKeySpec dpks = new DSAPublicKeySpec(// y
    new BigInteger("1"), // p
    new BigInteger("2"), // q
    new BigInteger("3"), // g
    new BigInteger("4"));
    assertEquals(4, dpks.getG().intValue());
}
Also used : BigInteger(java.math.BigInteger) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)63 BigInteger (java.math.BigInteger)45 KeyFactory (java.security.KeyFactory)37 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)23 PublicKey (java.security.PublicKey)22 DSAPublicKey (java.security.interfaces.DSAPublicKey)21 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)19 KeySpec (java.security.spec.KeySpec)19 DSAParams (java.security.interfaces.DSAParams)17 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)17 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)11 PrivateKey (java.security.PrivateKey)10 GeneralSecurityException (java.security.GeneralSecurityException)9 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)9 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)9 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)9 IOException (java.io.IOException)7 CertPathValidatorException (java.security.cert.CertPathValidatorException)7 InvalidKeyException (java.security.InvalidKeyException)5 KeyPair (java.security.KeyPair)5