Search in sources :

Example 46 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project remote-desktop-clients by iiordanov.

the class PubkeyUtils method recoverKeyPair.

public static KeyPair recoverKeyPair(byte[] encoded) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
    KeySpec privKeySpec = new PKCS8EncodedKeySpec(encoded);
    KeySpec pubKeySpec;
    PrivateKey priv;
    PublicKey pub;
    KeyFactory kf;
    try {
        kf = KeyFactory.getInstance(PubkeyDatabase.KEY_TYPE_RSA, "BC");
        priv = kf.generatePrivate(privKeySpec);
        pubKeySpec = new RSAPublicKeySpec(((RSAPrivateCrtKey) priv).getModulus(), ((RSAPrivateCrtKey) priv).getPublicExponent());
        pub = kf.generatePublic(pubKeySpec);
    } catch (ClassCastException e) {
        kf = KeyFactory.getInstance(PubkeyDatabase.KEY_TYPE_DSA, "BC");
        priv = kf.generatePrivate(privKeySpec);
        DSAParams params = ((DSAPrivateKey) priv).getParams();
        // Calculate public key Y
        BigInteger y = params.getG().modPow(((DSAPrivateKey) priv).getX(), params.getP());
        pubKeySpec = new DSAPublicKeySpec(y, params.getP(), params.getQ(), params.getG());
        pub = kf.generatePublic(pubKeySpec);
    }
    return new KeyPair(pub, priv);
}
Also used : KeyPair(java.security.KeyPair) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) DSAPublicKey(java.security.interfaces.DSAPublicKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAParams(java.security.interfaces.DSAParams) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) BigInteger(java.math.BigInteger) DSAPrivateKey(java.security.interfaces.DSAPrivateKey) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 47 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project keystore-explorer by kaikramer.

the class KeyPairUtil method getKeyInfo.

/**
 * Get the information about the supplied public key.
 *
 * @param publicKey
 *            The public key
 * @return Key information
 * @throws CryptoException
 *             If there is a problem getting the information
 */
public static KeyInfo getKeyInfo(PublicKey publicKey) throws CryptoException {
    try {
        String algorithm = publicKey.getAlgorithm();
        if (algorithm.equals(RSA.jce())) {
            KeyFactory keyFact = KeyFactory.getInstance(algorithm, BOUNCY_CASTLE.jce());
            RSAPublicKeySpec keySpec = keyFact.getKeySpec(publicKey, RSAPublicKeySpec.class);
            BigInteger modulus = keySpec.getModulus();
            return new KeyInfo(ASYMMETRIC, algorithm, modulus.toString(2).length());
        } else if (algorithm.equals(DSA.jce())) {
            KeyFactory keyFact = KeyFactory.getInstance(algorithm);
            DSAPublicKeySpec keySpec = keyFact.getKeySpec(publicKey, DSAPublicKeySpec.class);
            BigInteger prime = keySpec.getP();
            return new KeyInfo(ASYMMETRIC, algorithm, prime.toString(2).length());
        } else if (algorithm.equals(EC.jce())) {
            ECPublicKey pubk = (ECPublicKey) publicKey;
            int size = pubk.getParams().getOrder().bitLength();
            return new KeyInfo(ASYMMETRIC, algorithm, size);
        }
        // size unknown
        return new KeyInfo(ASYMMETRIC, algorithm);
    } catch (GeneralSecurityException ex) {
        throw new CryptoException(res.getString("NoPublicKeysize.exception.message"), ex);
    }
}
Also used : ECPublicKey(java.security.interfaces.ECPublicKey) KeyInfo(org.kse.crypto.KeyInfo) GeneralSecurityException(java.security.GeneralSecurityException) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) CryptoException(org.kse.crypto.CryptoException) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 48 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project j2objc by google.

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)

Example 49 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project j2objc by google.

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 50 with DSAPublicKeySpec

use of java.security.spec.DSAPublicKeySpec in project j2objc by google.

the class DSAPublicKeySpecTest method testGetP.

/**
 * Test for <code>getP</code> method
 */
public final void testGetP() {
    DSAPublicKeySpec dpks = new DSAPublicKeySpec(// y
    new BigInteger("1"), // p
    new BigInteger("2"), // q
    new BigInteger("3"), // g
    new BigInteger("4"));
    assertEquals(2, dpks.getP().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