Search in sources :

Example 6 with DSAPrivateKeySpec

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

the class OtrService method saveKey.

private void saveKey() {
    PublicKey publicKey = keyPair.getPublic();
    PrivateKey privateKey = keyPair.getPrivate();
    KeyFactory keyFactory;
    try {
        keyFactory = KeyFactory.getInstance("DSA");
        DSAPrivateKeySpec privateKeySpec = keyFactory.getKeySpec(privateKey, DSAPrivateKeySpec.class);
        DSAPublicKeySpec publicKeySpec = keyFactory.getKeySpec(publicKey, DSAPublicKeySpec.class);
        this.account.setKey("otr_x", privateKeySpec.getX().toString(16));
        this.account.setKey("otr_g", privateKeySpec.getG().toString(16));
        this.account.setKey("otr_p", privateKeySpec.getP().toString(16));
        this.account.setKey("otr_q", privateKeySpec.getQ().toString(16));
        this.account.setKey("otr_y", publicKeySpec.getY().toString(16));
    } catch (final NoSuchAlgorithmException | InvalidKeySpecException e) {
        e.printStackTrace();
    }
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) PrivateKey(java.security.PrivateKey) PublicKey(java.security.PublicKey) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 7 with DSAPrivateKeySpec

use of java.security.spec.DSAPrivateKeySpec in project XobotOS by xamarin.

the class DSAKeyFactoryImpl method engineGetKeySpec.

/**
     * This method returns a specification for the supplied key.
     *
     * The specification will be returned in the form of an object of the type
     * specified by keySpec.
     *
     * @param key -
     *            either DSAPrivateKey or DSAPublicKey
     * @param keySpec -
     *            either DSAPrivateKeySpec.class or DSAPublicKeySpec.class
     *
     * @return either a DSAPrivateKeySpec or a DSAPublicKeySpec
     *
     * @throws InvalidKeySpecException
     *             if "keySpec" is not a specification for DSAPublicKey or
     *             DSAPrivateKey
     */
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpec) throws InvalidKeySpecException {
    BigInteger p, q, g, x, y;
    if (key != null) {
        if (keySpec == null) {
            throw new NullPointerException("keySpec == null");
        }
        if (key instanceof DSAPrivateKey) {
            DSAPrivateKey privateKey = (DSAPrivateKey) key;
            if (keySpec.equals(DSAPrivateKeySpec.class)) {
                x = privateKey.getX();
                DSAParams params = privateKey.getParams();
                p = params.getP();
                q = params.getQ();
                g = params.getG();
                return (T) (new DSAPrivateKeySpec(x, p, q, g));
            }
            if (keySpec.equals(PKCS8EncodedKeySpec.class)) {
                return (T) (new PKCS8EncodedKeySpec(key.getEncoded()));
            }
            throw new InvalidKeySpecException("'keySpec' is neither DSAPrivateKeySpec nor PKCS8EncodedKeySpec");
        }
        if (key instanceof DSAPublicKey) {
            DSAPublicKey publicKey = (DSAPublicKey) key;
            if (keySpec.equals(DSAPublicKeySpec.class)) {
                y = publicKey.getY();
                DSAParams params = publicKey.getParams();
                p = params.getP();
                q = params.getQ();
                g = params.getG();
                return (T) (new DSAPublicKeySpec(y, p, q, g));
            }
            if (keySpec.equals(X509EncodedKeySpec.class)) {
                return (T) (new X509EncodedKeySpec(key.getEncoded()));
            }
            throw new InvalidKeySpecException("'keySpec' is neither DSAPublicKeySpec nor X509EncodedKeySpec");
        }
    }
    throw new InvalidKeySpecException("'key' is neither DSAPublicKey nor DSAPrivateKey");
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAParams(java.security.interfaces.DSAParams) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DSAPublicKey(java.security.interfaces.DSAPublicKey) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 8 with DSAPrivateKeySpec

use of java.security.spec.DSAPrivateKeySpec in project XobotOS by xamarin.

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 9 with DSAPrivateKeySpec

use of java.security.spec.DSAPrivateKeySpec in project jdk8u_jdk by JetBrains.

the class GenerationTests method getPrivateKey.

private static PrivateKey getPrivateKey(String algo, int keysize) throws Exception {
    KeyFactory kf = KeyFactory.getInstance(algo);
    KeySpec kspec;
    if (algo.equalsIgnoreCase("DSA")) {
        if (keysize == 1024) {
            kspec = new DSAPrivateKeySpec(new BigInteger(DSA_X), new BigInteger(DSA_P), new BigInteger(DSA_Q), new BigInteger(DSA_G));
        } else if (keysize == 2048) {
            kspec = new DSAPrivateKeySpec(new BigInteger(DSA_2048_X), new BigInteger(DSA_2048_P), new BigInteger(DSA_2048_Q), new BigInteger(DSA_2048_G));
        } else
            throw new RuntimeException("Unsupported keysize:" + keysize);
    } else if (algo.equalsIgnoreCase("RSA")) {
        if (keysize == 512) {
            kspec = new RSAPrivateKeySpec(new BigInteger(RSA_MOD), new BigInteger(RSA_PRIV));
        } else {
            kspec = new RSAPrivateKeySpec(new BigInteger(RSA_1024_MOD), new BigInteger(RSA_1024_PRIV));
        }
    } else
        throw new RuntimeException("Unsupported key algorithm " + algo);
    return kf.generatePrivate(kspec);
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) KeyFactory(java.security.KeyFactory)

Example 10 with DSAPrivateKeySpec

use of java.security.spec.DSAPrivateKeySpec in project jdk8u_jdk by JetBrains.

the class DSAKeyFactory 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 {
    DSAParams params;
    try {
        if (key instanceof java.security.interfaces.DSAPublicKey) {
            // Determine valid key specs
            Class<?> dsaPubKeySpec = Class.forName("java.security.spec.DSAPublicKeySpec");
            Class<?> x509KeySpec = Class.forName("java.security.spec.X509EncodedKeySpec");
            if (dsaPubKeySpec.isAssignableFrom(keySpec)) {
                java.security.interfaces.DSAPublicKey dsaPubKey = (java.security.interfaces.DSAPublicKey) key;
                params = dsaPubKey.getParams();
                return keySpec.cast(new DSAPublicKeySpec(dsaPubKey.getY(), params.getP(), params.getQ(), params.getG()));
            } else if (x509KeySpec.isAssignableFrom(keySpec)) {
                return keySpec.cast(new X509EncodedKeySpec(key.getEncoded()));
            } else {
                throw new InvalidKeySpecException("Inappropriate key specification");
            }
        } else if (key instanceof java.security.interfaces.DSAPrivateKey) {
            // Determine valid key specs
            Class<?> dsaPrivKeySpec = Class.forName("java.security.spec.DSAPrivateKeySpec");
            Class<?> pkcs8KeySpec = Class.forName("java.security.spec.PKCS8EncodedKeySpec");
            if (dsaPrivKeySpec.isAssignableFrom(keySpec)) {
                java.security.interfaces.DSAPrivateKey dsaPrivKey = (java.security.interfaces.DSAPrivateKey) key;
                params = dsaPrivKey.getParams();
                return keySpec.cast(new DSAPrivateKeySpec(dsaPrivKey.getX(), params.getP(), params.getQ(), params.getG()));
            } else if (pkcs8KeySpec.isAssignableFrom(keySpec)) {
                return keySpec.cast(new PKCS8EncodedKeySpec(key.getEncoded()));
            } else {
                throw new InvalidKeySpecException("Inappropriate key specification");
            }
        } else {
            throw new InvalidKeySpecException("Inappropriate key type");
        }
    } catch (ClassNotFoundException e) {
        throw new InvalidKeySpecException("Unsupported key specification: " + e.getMessage());
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAParams(java.security.interfaces.DSAParams) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Aggregations

DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)21 BigInteger (java.math.BigInteger)14 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)11 DSAPrivateKey (java.security.interfaces.DSAPrivateKey)10 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)9 PrivateKey (java.security.PrivateKey)7 DSAParams (java.security.interfaces.DSAParams)7 KeyFactory (java.security.KeyFactory)6 DSAPublicKey (java.security.interfaces.DSAPublicKey)6 PublicKey (java.security.PublicKey)5 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)5 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)5 InvalidKeyException (java.security.InvalidKeyException)3 KeyStore (java.security.KeyStore)3 KeyStoreException (java.security.KeyStoreException)3 Certificate (java.security.cert.Certificate)3 X509Certificate (java.security.cert.X509Certificate)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 Signature (java.security.Signature)2 KeySpec (java.security.spec.KeySpec)2