Search in sources :

Example 46 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project azure-sdk-for-java by Azure.

the class JsonWebKey method getRSAPrivateKey.

/**
     * Get the RSA private key value.
     *
     * @param provider the Java security provider.
     * @return the RSA private key value
     */
private PrivateKey getRSAPrivateKey(Provider provider) {
    try {
        RSAPrivateKeySpec privateKeySpec = getRSAPrivateKeySpec();
        KeyFactory factory = provider != null ? KeyFactory.getInstance("RSA", provider) : KeyFactory.getInstance("RSA");
        return factory.generatePrivate(privateKeySpec);
    } catch (GeneralSecurityException e) {
        throw new IllegalStateException(e);
    }
}
Also used : RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) KeyFactory(java.security.KeyFactory)

Example 47 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project oxAuth by GluuFederation.

the class RSASigner method sign.

@Override
public String sign(String signingInput) throws Exception {
    if (Strings.isNullOrEmpty(signingInput)) {
        throw new Exception("Invalid signing input");
    }
    try {
        RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
        PrivateKey privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec);
        Signature signature = Signature.getInstance(getSignatureAlgorithm().getAlgorithm(), "BC");
        signature.initSign(privateKey);
        signature.update(signingInput.getBytes(Util.UTF8_STRING_ENCODING));
        return Base64Util.base64urlencode(signature.sign());
    } catch (NoSuchAlgorithmException e) {
        throw new Exception("There was a problem in RSA signing", e);
    } catch (UnsupportedEncodingException e) {
        throw new Exception("There was a problem in RSA signing", e);
    } catch (SignatureException e) {
        throw new Exception("There was a problem in RSA signing", e);
    } catch (NoSuchProviderException e) {
        throw new Exception("There was a problem in RSA signing", e);
    } catch (InvalidKeyException e) {
        throw new Exception("There was a problem in RSA signing", e);
    } catch (InvalidKeySpecException e) {
        throw new Exception("There was a problem in RSA signing", e);
    }
}
Also used : RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 48 with RSAPrivateKeySpec

use of java.security.spec.RSAPrivateKeySpec in project oxAuth by GluuFederation.

the class RSASigner method generateSignature.

@Override
public String generateSignature(String signingInput) throws SignatureException {
    if (getSignatureAlgorithm() == null) {
        throw new SignatureException("The signature algorithm is null");
    }
    if (rsaPrivateKey == null) {
        throw new SignatureException("The RSA private key is null");
    }
    if (signingInput == null) {
        throw new SignatureException("The signing input is null");
    }
    try {
        RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
        KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
        PrivateKey privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec);
        Signature signature = Signature.getInstance(getSignatureAlgorithm().getAlgorithm(), "BC");
        signature.initSign(privateKey);
        signature.update(signingInput.getBytes(Util.UTF8_STRING_ENCODING));
        return Base64Util.base64urlencode(signature.sign());
    } catch (InvalidKeySpecException e) {
        throw new SignatureException(e);
    } catch (InvalidKeyException e) {
        throw new SignatureException(e);
    } catch (NoSuchAlgorithmException e) {
        throw new SignatureException(e);
    } catch (NoSuchProviderException e) {
        throw new SignatureException(e);
    } catch (SignatureException e) {
        throw new SignatureException(e);
    } catch (UnsupportedEncodingException e) {
        throw new SignatureException(e);
    } catch (Exception e) {
        throw new SignatureException(e);
    }
}
Also used : RSAPrivateKey(org.xdi.oxauth.model.crypto.signature.RSAPrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) UnsupportedEncodingException(java.io.UnsupportedEncodingException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 49 with RSAPrivateKeySpec

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

the class KeySizeTest method main.

public static void main(String[] args) throws Exception {
    int iKeyPairSize = Integer.parseInt(args[0]);
    int maxLoopCnt = Integer.parseInt(args[1]);
    int failCount = 0;
    KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
    keyPairGen.initialize(iKeyPairSize);
    // Generate RSA keypair
    KeyPair keyPair = keyPairGen.generateKeyPair();
    // Get priavte and public keys
    PrivateKey privateKey = keyPair.getPrivate();
    PublicKey publicKey = keyPair.getPublic();
    try {
        if (!sizeTest(keyPair)) {
            failCount++;
        }
    } catch (Exception ex) {
        ex.printStackTrace(System.err);
        failCount++;
    }
    for (int iCnt = 0; iCnt < maxLoopCnt; iCnt++) {
        // Get keysize (modulus) of keys
        KeyFactory keyFact = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
        // Comparing binary length.
        RSAPrivateKeySpec privateKeySpec = (RSAPrivateKeySpec) keyFact.getKeySpec(privateKey, RSAPrivateKeySpec.class);
        int iPrivateKeySize = privateKeySpec.getModulus().bitLength();
        RSAPublicKeySpec publicKeySpec = (RSAPublicKeySpec) keyFact.getKeySpec(publicKey, RSAPublicKeySpec.class);
        int iPublicKeySize = publicKeySpec.getModulus().bitLength();
        if ((iKeyPairSize != iPublicKeySize) || (iKeyPairSize != iPrivateKeySize)) {
            System.err.println("iKeyPairSize : " + iKeyPairSize);
            System.err.println("Generated a " + iPrivateKeySize + " bit RSA private key");
            System.err.println("Generated a " + iPublicKeySize + " bit RSA public key");
            failCount++;
        }
    }
    if (failCount > 0) {
        throw new RuntimeException("There are " + failCount + " tests failed.");
    }
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) KeyPairGenerator(java.security.KeyPairGenerator) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 50 with RSAPrivateKeySpec

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

the class PrivateKeyEqualityTest method main.

public static void main(String[] args) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeySpecException {
    // Generate the first key.
    KeyPairGenerator generator = KeyPairGenerator.getInstance(KEYALG, PROVIDER_NAME);
    KeyPair keyPair = generator.generateKeyPair();
    RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyPair.getPrivate();
    if (!(rsaPrivateKey instanceof RSAPrivateCrtKey)) {
        System.err.println("rsaPrivateKey class : " + rsaPrivateKey.getClass().getName());
        throw new RuntimeException("rsaPrivateKey is not a RSAPrivateCrtKey instance");
    }
    // Generate the second key.
    KeyFactory factory = KeyFactory.getInstance(KEYALG, PROVIDER_NAME);
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent());
    RSAPrivateKey rsaPrivateKey2 = (RSAPrivateKey) factory.generatePrivate(rsaPrivateKeySpec);
    // Generate the third key.
    PKCS8EncodedKeySpec encodedKeySpec = new PKCS8EncodedKeySpec(rsaPrivateKey.getEncoded());
    RSAPrivateKey rsaPrivateKey3 = (RSAPrivateKey) factory.generatePrivate(encodedKeySpec);
    // Check for equality.
    if (rsaPrivateKey.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey should not equal to rsaPrivateKey2");
    }
    if (!rsaPrivateKey3.equals(rsaPrivateKey)) {
        throw new RuntimeException("rsaPrivateKey3 should equal to rsaPrivateKey");
    }
    if (rsaPrivateKey3.equals(rsaPrivateKey2)) {
        throw new RuntimeException("rsaPrivateKey3 should not equal to rsaPrivateKey2");
    }
    if (rsaPrivateKey2.equals(rsaPrivateKey3)) {
        throw new RuntimeException("rsaPrivateKey2 should not equal to rsaPrivateKey3");
    }
    // Generate the fourth key.
    RSAPrivateCrtKey rsaPrivateCrtKey = (RSAPrivateCrtKey) rsaPrivateKey;
    RSAPrivateCrtKeySpec rsaPrivateCrtKeySpec = new RSAPrivateCrtKeySpec(rsaPrivateCrtKey.getModulus(), rsaPrivateCrtKey.getPublicExponent(), rsaPrivateCrtKey.getPrivateExponent(), rsaPrivateCrtKey.getPrimeP(), rsaPrivateCrtKey.getPrimeQ(), rsaPrivateCrtKey.getPrimeExponentP(), rsaPrivateCrtKey.getPrimeExponentQ(), rsaPrivateCrtKey.getCrtCoefficient());
    RSAPrivateCrtKey rsaPrivateKey4 = (RSAPrivateCrtKey) factory.generatePrivate(rsaPrivateCrtKeySpec);
    if (!rsaPrivateKey.equals(rsaPrivateKey4)) {
        throw new RuntimeException("rsaPrivateKey should equal to rsaPrivateKey4");
    }
}
Also used : RSAPrivateCrtKeySpec(java.security.spec.RSAPrivateCrtKeySpec) KeyPair(java.security.KeyPair) RSAPrivateCrtKey(java.security.interfaces.RSAPrivateCrtKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeyPairGenerator(java.security.KeyPairGenerator) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) KeyFactory(java.security.KeyFactory)

Aggregations

RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)50 KeyFactory (java.security.KeyFactory)30 PrivateKey (java.security.PrivateKey)28 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)21 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)17 PublicKey (java.security.PublicKey)16 Signature (java.security.Signature)10 Cipher (javax.crypto.Cipher)10 BigInteger (java.math.BigInteger)9 RSAPublicKey (java.security.interfaces.RSAPublicKey)9 SecretKeyFactory (javax.crypto.SecretKeyFactory)9 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)8 KeySpec (java.security.spec.KeySpec)7 RSAPrivateCrtKeySpec (java.security.spec.RSAPrivateCrtKeySpec)7 InvalidKeyException (java.security.InvalidKeyException)5 RSAPrivateCrtKey (java.security.interfaces.RSAPrivateCrtKey)5 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)5 KeyPairGenerator (java.security.KeyPairGenerator)4 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)4 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)4