Search in sources :

Example 21 with DSAPrivateKeySpec

use of java.security.spec.DSAPrivateKeySpec 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)

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