Search in sources :

Example 81 with RSAPublicKeySpec

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

the class RSANoLimit method main.

public static void main(String[] args) throws Exception {
    boolean result = true;
    Provider p = Security.getProvider("SunJCE");
    System.out.println("Testing provider " + p.getName() + "...");
    // correct value
    if (Cipher.getMaxAllowedKeyLength("RSA") != Integer.MAX_VALUE) {
        result = false;
        System.out.println("Test#1 failed");
    }
    // Test#2: try initializing RSA cipher with 4096 key
    String algo = "RSA";
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(new BigInteger(MODULUS4096), new BigInteger(PUB4096));
    KeyFactory kf = KeyFactory.getInstance(algo);
    RSAPublicKey pubKey = (RSAPublicKey) kf.generatePublic(pubKeySpec);
    Cipher c = Cipher.getInstance(algo + "/ECB/NoPadding", p);
    try {
        c.init(Cipher.ENCRYPT_MODE, pubKey);
    } catch (InvalidKeyException ike) {
        result = false;
        System.out.println("Test#2 failed");
        ike.printStackTrace();
    }
    if (result) {
        System.out.println("All tests passed!");
    } else {
        throw new Exception("One or more test failed!");
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 82 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project JustAndroid by chinaltz.

the class AbRsa 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(ALGORITHM);
    PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
    return privateKey;
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 83 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project oxTrust by GluuFederation.

the class ManageCertificateAction method getKeyPair.

private KeyPair getKeyPair(String fileName) {
    KeyPair pair = null;
    JCERSAPrivateCrtKey privateKey = null;
    PEMParser r = null;
    FileReader fileReader = null;
    File keyFile = new File(getTempCertDir() + fileName.replace("crt", "key"));
    if (keyFile.isFile()) {
        try {
            fileReader = new FileReader(keyFile);
            r = new PEMParser(fileReader);
            Object keys = r.readObject();
            if (keys == null) {
                log.error(" Unable to read keys from: " + keyFile.getAbsolutePath());
                return null;
            }
            if (keys instanceof KeyPair) {
                pair = (KeyPair) keys;
                log.debug(keyFile.getAbsolutePath() + "contains KeyPair");
            } else if (keys instanceof JCERSAPrivateCrtKey) {
                privateKey = (JCERSAPrivateCrtKey) keys;
                log.debug(keyFile.getAbsolutePath() + "contains JCERSAPrivateCrtKey");
                BigInteger exponent = privateKey.getPublicExponent();
                BigInteger modulus = privateKey.getModulus();
                RSAPublicKeySpec publicKeySpec = new java.security.spec.RSAPublicKeySpec(modulus, exponent);
                PublicKey publicKey = null;
                try {
                    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
                    publicKey = keyFactory.generatePublic(publicKeySpec);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                pair = new KeyPair(publicKey, privateKey);
            } else {
                log.error(keyFile.getAbsolutePath() + " Contains unsupported key type: " + keys.getClass().getName());
                return null;
            }
        } catch (IOException e) {
            log.error(e.getMessage(), e);
            return null;
        } finally {
            try {
                r.close();
                fileReader.close();
            } catch (Exception e) {
                log.error(e.getMessage(), e);
                return null;
            }
        }
    } else {
        log.error("Key file does not exist : " + keyFile.getAbsolutePath());
    }
    log.debug("KeyPair successfully extracted from: " + keyFile.getAbsolutePath());
    return pair;
}
Also used : KeyPair(java.security.KeyPair) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException) BaseMappingException(org.gluu.persist.exception.mapping.BaseMappingException) IOException(java.io.IOException) PEMParser(org.bouncycastle.openssl.PEMParser) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) BigInteger(java.math.BigInteger) FileReader(java.io.FileReader) JCERSAPrivateCrtKey(org.bouncycastle.jce.provider.JCERSAPrivateCrtKey) UploadedFile(org.richfaces.model.UploadedFile) File(java.io.File) KeyFactory(java.security.KeyFactory)

Example 84 with RSAPublicKeySpec

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

the class HttpTestUtil method generateKeyPair.

private KeyPair generateKeyPair() {
    try {
        KeyPair seed = KeyPairGenerator.getInstance("RSA", "BC").generateKeyPair();
        RSAPrivateKey privateSeed = (RSAPrivateKey) seed.getPrivate();
        RSAPublicKey publicSeed = (RSAPublicKey) seed.getPublic();
        KeyFactory fact = KeyFactory.getInstance("RSA", "BC");
        RSAPrivateKeySpec privateKeySpec = new RSAPrivateKeySpec(privateSeed.getModulus(), privateSeed.getPrivateExponent());
        RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(publicSeed.getModulus(), publicSeed.getPublicExponent());
        return new KeyPair(fact.generatePublic(publicKeySpec), fact.generatePrivate(privateKeySpec));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 85 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project athenz by yahoo.

the class Crypto method extractPublicKey.

public static PublicKey extractPublicKey(PrivateKey privateKey) throws CryptoException {
    // we only support RSA and ECDSA private keys
    PublicKey publicKey = null;
    switch(privateKey.getAlgorithm()) {
        case RSA:
            try {
                KeyFactory kf = KeyFactory.getInstance(RSA, BC_PROVIDER);
                RSAPrivateCrtKey rsaCrtKey = (RSAPrivateCrtKey) privateKey;
                RSAPublicKeySpec keySpec = new RSAPublicKeySpec(rsaCrtKey.getModulus(), rsaCrtKey.getPublicExponent());
                publicKey = kf.generatePublic(keySpec);
            } catch (NoSuchProviderException ex) {
                LOG.error("extractPublicKey: RSA - Caught NoSuchProviderException exception: " + ex.getMessage());
                throw new CryptoException(ex);
            } catch (NoSuchAlgorithmException ex) {
                LOG.error("extractPublicKey: RSA - Caught NoSuchAlgorithmException exception: " + ex.getMessage());
                throw new CryptoException(ex);
            } catch (InvalidKeySpecException ex) {
                LOG.error("extractPublicKey: RSA - Caught InvalidKeySpecException exception: " + ex.getMessage());
                throw new CryptoException(ex);
            }
            break;
        case ECDSA:
            try {
                KeyFactory kf = KeyFactory.getInstance(ECDSA, BC_PROVIDER);
                BCECPrivateKey ecPrivKey = (BCECPrivateKey) privateKey;
                ECMultiplier ecMultiplier = new FixedPointCombMultiplier();
                ECParameterSpec ecParamSpec = (ECParameterSpec) ecPrivKey.getParameters();
                ECPoint ecPointQ = ecMultiplier.multiply(ecParamSpec.getG(), ecPrivKey.getD());
                ECPublicKeySpec keySpec = new ECPublicKeySpec(ecPointQ, ecParamSpec);
                publicKey = kf.generatePublic(keySpec);
            } catch (NoSuchProviderException ex) {
                LOG.error("extractPublicKey: ECDSA - Caught NoSuchProviderException exception: " + ex.getMessage());
                throw new CryptoException(ex);
            } catch (NoSuchAlgorithmException ex) {
                LOG.error("extractPublicKey: ECDSA - Caught NoSuchAlgorithmException exception: " + ex.getMessage());
                throw new CryptoException(ex);
            } catch (InvalidKeySpecException ex) {
                LOG.error("extractPublicKey: ECDSA - Caught InvalidKeySpecException exception: " + ex.getMessage());
                throw new CryptoException(ex);
            }
            break;
        default:
            String msg = "Unsupported Key Algorithm: " + privateKey.getAlgorithm();
            LOG.error("extractPublicKey: " + msg);
            throw new CryptoException(msg);
    }
    return publicKey;
}
Also used : RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) BCECPublicKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPublicKey) PublicKey(java.security.PublicKey) ECMultiplier(org.bouncycastle.math.ec.ECMultiplier) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) DEROctetString(org.bouncycastle.asn1.DEROctetString) DERIA5String(org.bouncycastle.asn1.DERIA5String) ECPoint(org.bouncycastle.math.ec.ECPoint) ECPublicKeySpec(org.bouncycastle.jce.spec.ECPublicKeySpec) BCECPrivateKey(org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey) FixedPointCombMultiplier(org.bouncycastle.math.ec.FixedPointCombMultiplier) ECParameterSpec(org.bouncycastle.jce.spec.ECParameterSpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchProviderException(java.security.NoSuchProviderException) KeyFactory(java.security.KeyFactory)

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