Search in sources :

Example 36 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project smartmodule by carozhu.

the class RSAUtils method getPrivateKey.

/**
	 * 使用N、d值还原私钥
	 * 
	 * @param modulus
	 * @param privateExponent
	 * @return
	 * @throws NoSuchAlgorithmException
	 * @throws InvalidKeySpecException
	 */
public static PrivateKey getPrivateKey(String modulus, String privateExponent) throws NoSuchAlgorithmException, InvalidKeySpecException {
    BigInteger bigIntModulus = new BigInteger(modulus);
    BigInteger bigIntPrivateExponent = new BigInteger(privateExponent);
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(bigIntModulus, bigIntPrivateExponent);
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_PAIR);
    PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
    return privateKey;
}
Also used : PrivateKey(java.security.PrivateKey) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 37 with RSAPublicKeySpec

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

the class RSAKeyValue method getPublicKey.

/** @inheritDoc */
public PublicKey getPublicKey() throws XMLSecurityException {
    try {
        KeyFactory rsaFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec rsaKeyspec = new RSAPublicKeySpec(this.getBigIntegerFromChildElement(Constants._TAG_MODULUS, Constants.SignatureSpecNS), this.getBigIntegerFromChildElement(Constants._TAG_EXPONENT, Constants.SignatureSpecNS));
        PublicKey pk = rsaFactory.generatePublic(rsaKeyspec);
        return pk;
    } catch (NoSuchAlgorithmException ex) {
        throw new XMLSecurityException("empty", ex);
    } catch (InvalidKeySpecException ex) {
        throw new XMLSecurityException("empty", ex);
    }
}
Also used : PublicKey(java.security.PublicKey) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) XMLSecurityException(com.sun.org.apache.xml.internal.security.exceptions.XMLSecurityException)

Example 38 with RSAPublicKeySpec

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

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 39 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project verify-hub by alphagov.

the class Crypto method keyPairFromPrivateKey.

public static KeyPair keyPairFromPrivateKey(PrivateKey privateKey) {
    try {
        RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPublicExponent());
        PublicKey publicKey = KeyFactory.getInstance("RSA").generatePublic(keySpec);
        return new KeyPair(publicKey, privateKey);
    } catch (ClassCastException ex) {
        throw new RuntimeException("Private key must be RSA format");
    } catch (Exception ex) {
        throw new RuntimeException("Could not get public key from private key");
    }
}
Also used : KeyPair(java.security.KeyPair) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 40 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project baseio by generallycloud.

the class RSAUtil method getPublicKey.

/**
 * 使用模和指数生成RSA公钥
 * 注意:【此代码用了默认补位方式,为RSA/None/PKCS1Padding,不同JDK默认的补位方式可能不同,如Android默认是RSA
 * /None/NoPadding】
 *
 * @param modulus
 *             模
 * @param exponent
 *             指数
 * @return
 */
public static RSAPublicKey getPublicKey(String modulus, String exponent) {
    try {
        BigInteger b1 = new BigInteger(modulus);
        BigInteger b2 = new BigInteger(exponent);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(b1, b2);
        return (RSAPublicKey) keyFactory.generatePublic(keySpec);
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)85 KeyFactory (java.security.KeyFactory)61 PublicKey (java.security.PublicKey)48 RSAPublicKey (java.security.interfaces.RSAPublicKey)30 BigInteger (java.math.BigInteger)24 Signature (java.security.Signature)22 PrivateKey (java.security.PrivateKey)20 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)17 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)14 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)12 Cipher (javax.crypto.Cipher)12 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)10 SecretKeyFactory (javax.crypto.SecretKeyFactory)10 IOException (java.io.IOException)9 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)9 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)8 KeySpec (java.security.spec.KeySpec)8 KeyPair (java.security.KeyPair)7 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)6 InvalidKeyException (java.security.InvalidKeyException)5