Search in sources :

Example 21 with RSAPublicKeySpec

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

the class CipherTest method testRSA_ECB_NoPadding_GetParameters_NoneProvided_Success.

private void testRSA_ECB_NoPadding_GetParameters_NoneProvided_Success(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    final PublicKey pubKey = kf.generatePublic(pubKeySpec);
    Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
    assertNull("Parameters should be null", c.getParameters());
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 22 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project platform_frameworks_base by android.

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)

Example 23 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project PushSms by koush.

the class Registration method parse.

static Registration parse(String data) {
    try {
        byte[] bytes = Base64.decode(data, Base64.NO_WRAP);
        BEncodedDictionary dict = BEncodedDictionary.parseDictionary(ByteBuffer.wrap(bytes));
        Registration ret = new Registration();
        ret.date = dict.getLong("date");
        ret.registrationId = dict.getString("registration_id");
        ret.endpoint = dict.getString("endpoint");
        ret.localSequenceNumber = dict.getInt("local_sequence_number");
        ret.remoteSequenceNumber = dict.getInt("remote_sequence_number");
        ret.state = dict.getInt("state");
        byte[] publicModulus = dict.getBytes("public_modulus");
        byte[] publicExponent = dict.getBytes("public_exponent");
        if (publicModulus != null && publicExponent != null) {
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            RSAPublicKeySpec rsaPublicKeySpec = new RSAPublicKeySpec(new BigInteger(publicModulus), new BigInteger(publicExponent));
            ret.remotePublicKey = keyFactory.generatePublic(rsaPublicKeySpec);
        }
        return ret;
    } catch (Exception e) {
        return null;
    }
}
Also used : BigInteger(java.math.BigInteger) BEncodedDictionary(org.cyanogenmod.pushsms.bencode.BEncodedDictionary) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 24 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project spring-security-oauth by spring-projects.

the class KeyStoreKeyFactory method getKeyPair.

public KeyPair getKeyPair(String alias, char[] password) {
    try {
        synchronized (lock) {
            if (store == null) {
                synchronized (lock) {
                    store = KeyStore.getInstance("jks");
                    store.load(resource.getInputStream(), this.password);
                }
            }
        }
        RSAPrivateCrtKey key = (RSAPrivateCrtKey) store.getKey(alias, password);
        RSAPublicKeySpec spec = new RSAPublicKeySpec(key.getModulus(), key.getPublicExponent());
        PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(spec);
        return new KeyPair(publicKey, key);
    } catch (Exception e) {
        throw new IllegalStateException("Cannot load keys from store: " + resource, e);
    }
}
Also used : KeyPair(java.security.KeyPair) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 25 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project spring-security-oauth by spring-projects.

the class JwkDefinitionSource method createRsaVerifier.

private static RsaVerifier createRsaVerifier(RsaJwkDefinition rsaDefinition) {
    RsaVerifier result;
    try {
        BigInteger modulus = new BigInteger(1, Codecs.b64UrlDecode(rsaDefinition.getModulus()));
        BigInteger exponent = new BigInteger(1, Codecs.b64UrlDecode(rsaDefinition.getExponent()));
        RSAPublicKey rsaPublicKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new RSAPublicKeySpec(modulus, exponent));
        result = new RsaVerifier(rsaPublicKey, rsaDefinition.getAlgorithm().standardName());
    } catch (Exception ex) {
        throw new JwkException("An error occurred while creating a RSA Public Key Verifier for " + rsaDefinition.getKeyId() + " : " + ex.getMessage(), ex);
    }
    return result;
}
Also used : RsaVerifier(org.springframework.security.jwt.crypto.sign.RsaVerifier) RSAPublicKey(java.security.interfaces.RSAPublicKey) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException)

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