Search in sources :

Example 26 with RSAPublicKeySpec

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

the class RSAPublicKeySpecTest method testGetPublicExponent.

/**
     * Test for <code>getPublicExponent()</code> method<br>
     * Assertion: returns public exponent
     */
public final void testGetPublicExponent() {
    RSAPublicKeySpec rpks = new RSAPublicKeySpec(BigInteger.valueOf(3L), BigInteger.valueOf(1234567890L));
    assertTrue(BigInteger.valueOf(1234567890L).equals(rpks.getPublicExponent()));
}
Also used : RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 27 with RSAPublicKeySpec

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

the class RSAPublicKeySpecTest method testRSAPublicKeySpec01.

/**
     * Test #1 for <code>RSAPublicKeySpec</code> constructor
     * Assertion: Constructs <code>RSAPublicKeySpec</code>
     * object using valid parameters
     */
public final void testRSAPublicKeySpec01() {
    KeySpec ks = new RSAPublicKeySpec(BigInteger.valueOf(1234567890L), BigInteger.valueOf(3L));
    assertTrue(ks instanceof RSAPublicKeySpec);
}
Also used : KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 28 with RSAPublicKeySpec

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

the class RSAPublicKeySpecTest method testGetModulus.

/**
     * Test for <code>getModulus()</code> method<br>
     * Assertion: returns modulus
     */
public final void testGetModulus() {
    RSAPublicKeySpec rpks = new RSAPublicKeySpec(BigInteger.valueOf(1234567890L), BigInteger.valueOf(3L));
    assertTrue(BigInteger.valueOf(1234567890L).equals(rpks.getModulus()));
}
Also used : RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 29 with RSAPublicKeySpec

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

the class GenerationTests method getPublicKey.

private static PublicKey getPublicKey(String algo, int keysize) throws Exception {
    KeyFactory kf = KeyFactory.getInstance(algo);
    KeySpec kspec;
    if (algo.equalsIgnoreCase("DSA")) {
        if (keysize == 1024) {
            kspec = new DSAPublicKeySpec(new BigInteger(DSA_Y), new BigInteger(DSA_P), new BigInteger(DSA_Q), new BigInteger(DSA_G));
        } else if (keysize == 2048) {
            kspec = new DSAPublicKeySpec(new BigInteger(DSA_2048_Y), 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 RSAPublicKeySpec(new BigInteger(RSA_MOD), new BigInteger(RSA_PUB));
        } else if (keysize == 1024) {
            kspec = new RSAPublicKeySpec(new BigInteger(RSA_1024_MOD), new BigInteger(RSA_PUB));
        } else
            throw new RuntimeException("Unsupported keysize:" + keysize);
    } else
        throw new RuntimeException("Unsupported key algorithm " + algo);
    return kf.generatePublic(kspec);
}
Also used : 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) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 30 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project android_frameworks_base by ResurrectionRemix.

the class AndroidKeyStoreKeyFactorySpi method engineGetKeySpec.

@Override
protected <T extends KeySpec> T engineGetKeySpec(Key key, Class<T> keySpecClass) throws InvalidKeySpecException {
    if (key == null) {
        throw new InvalidKeySpecException("key == null");
    } else if ((!(key instanceof AndroidKeyStorePrivateKey)) && (!(key instanceof AndroidKeyStorePublicKey))) {
        throw new InvalidKeySpecException("Unsupported key type: " + key.getClass().getName() + ". This KeyFactory supports only Android Keystore asymmetric keys");
    }
    if (keySpecClass == null) {
        throw new InvalidKeySpecException("keySpecClass == null");
    } else if (KeyInfo.class.equals(keySpecClass)) {
        if (!(key instanceof AndroidKeyStorePrivateKey)) {
            throw new InvalidKeySpecException("Unsupported key type: " + key.getClass().getName() + ". KeyInfo can be obtained only for Android Keystore private keys");
        }
        AndroidKeyStorePrivateKey keystorePrivateKey = (AndroidKeyStorePrivateKey) key;
        String keyAliasInKeystore = keystorePrivateKey.getAlias();
        String entryAlias;
        if (keyAliasInKeystore.startsWith(Credentials.USER_PRIVATE_KEY)) {
            entryAlias = keyAliasInKeystore.substring(Credentials.USER_PRIVATE_KEY.length());
        } else {
            throw new InvalidKeySpecException("Invalid key alias: " + keyAliasInKeystore);
        }
        @SuppressWarnings("unchecked") T result = (T) AndroidKeyStoreSecretKeyFactorySpi.getKeyInfo(mKeyStore, entryAlias, keyAliasInKeystore, keystorePrivateKey.getUid());
        return result;
    } else if (X509EncodedKeySpec.class.equals(keySpecClass)) {
        if (!(key instanceof AndroidKeyStorePublicKey)) {
            throw new InvalidKeySpecException("Unsupported key type: " + key.getClass().getName() + ". X509EncodedKeySpec can be obtained only for Android Keystore public" + " keys");
        }
        @SuppressWarnings("unchecked") T result = (T) new X509EncodedKeySpec(((AndroidKeyStorePublicKey) key).getEncoded());
        return result;
    } else if (PKCS8EncodedKeySpec.class.equals(keySpecClass)) {
        if (key instanceof AndroidKeyStorePrivateKey) {
            throw new InvalidKeySpecException("Key material export of Android Keystore private keys is not supported");
        } else {
            throw new InvalidKeySpecException("Cannot export key material of public key in PKCS#8 format." + " Only X.509 format (X509EncodedKeySpec) supported for public keys.");
        }
    } else if (RSAPublicKeySpec.class.equals(keySpecClass)) {
        if (key instanceof AndroidKeyStoreRSAPublicKey) {
            AndroidKeyStoreRSAPublicKey rsaKey = (AndroidKeyStoreRSAPublicKey) key;
            @SuppressWarnings("unchecked") T result = (T) new RSAPublicKeySpec(rsaKey.getModulus(), rsaKey.getPublicExponent());
            return result;
        } else {
            throw new InvalidKeySpecException("Obtaining RSAPublicKeySpec not supported for " + key.getAlgorithm() + " " + ((key instanceof AndroidKeyStorePrivateKey) ? "private" : "public") + " key");
        }
    } else if (ECPublicKeySpec.class.equals(keySpecClass)) {
        if (key instanceof AndroidKeyStoreECPublicKey) {
            AndroidKeyStoreECPublicKey ecKey = (AndroidKeyStoreECPublicKey) key;
            @SuppressWarnings("unchecked") T result = (T) new ECPublicKeySpec(ecKey.getW(), ecKey.getParams());
            return result;
        } else {
            throw new InvalidKeySpecException("Obtaining ECPublicKeySpec not supported for " + key.getAlgorithm() + " " + ((key instanceof AndroidKeyStorePrivateKey) ? "private" : "public") + " key");
        }
    } else {
        throw new InvalidKeySpecException("Unsupported key spec: " + keySpecClass.getName());
    }
}
Also used : X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) ECPublicKeySpec(java.security.spec.ECPublicKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Aggregations

RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)82 KeyFactory (java.security.KeyFactory)59 PublicKey (java.security.PublicKey)46 RSAPublicKey (java.security.interfaces.RSAPublicKey)29 BigInteger (java.math.BigInteger)23 Signature (java.security.Signature)22 PrivateKey (java.security.PrivateKey)20 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)17 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)13 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)12 Cipher (javax.crypto.Cipher)12 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)10 SecretKeyFactory (javax.crypto.SecretKeyFactory)10 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)9 IOException (java.io.IOException)8 KeySpec (java.security.spec.KeySpec)8 KeyPair (java.security.KeyPair)6 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)6 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)6 InvalidKeyException (java.security.InvalidKeyException)5