Search in sources :

Example 31 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 32 with RSAPrivateKeySpec

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

the class SignatureTest method testSign_SHA1withRSA_Key_Success.

public void testSign_SHA1withRSA_Key_Success() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    PrivateKey privKey = kf.generatePrivate(keySpec);
    Signature sig = Signature.getInstance("SHA1withRSA");
    sig.initSign(privKey);
    sig.update(Vector1Data);
    byte[] signature = sig.sign();
    assertNotNull("Signature must not be null", signature);
    assertTrue("Signature should match expected", Arrays.equals(signature, SHA1withRSA_Vector1Signature));
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    sig.initVerify(pubKey);
    sig.update(Vector1Data);
    assertTrue("Signature must verify correctly", sig.verify(signature));
}
Also used : PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PublicKey(java.security.PublicKey) Signature(java.security.Signature) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 33 with RSAPrivateKeySpec

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

the class SignatureTest method testSign_SHA512withRSA_Key_Success.

public void testSign_SHA512withRSA_Key_Success() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    PrivateKey privKey = kf.generatePrivate(keySpec);
    Signature sig = Signature.getInstance("SHA512withRSA");
    sig.initSign(privKey);
    sig.update(Vector2Data);
    byte[] signature = sig.sign();
    assertNotNull("Signature must not be null", signature);
    assertTrue("Signature should match expected", Arrays.equals(signature, SHA512withRSA_Vector2Signature));
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    PublicKey pubKey = kf.generatePublic(pubKeySpec);
    sig.initVerify(pubKey);
    sig.update(Vector2Data);
    assertTrue("Signature must verify correctly", sig.verify(signature));
}
Also used : PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) PublicKey(java.security.PublicKey) Signature(java.security.Signature) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 34 with RSAPrivateKeySpec

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

the class SignatureTest method testSign_NONEwithRSA_Key_DataTooLarge_Failure.

public void testSign_NONEwithRSA_Key_DataTooLarge_Failure() throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    PrivateKey privKey = kf.generatePrivate(keySpec);
    Signature sig = Signature.getInstance("NONEwithRSA");
    sig.initSign(privKey);
    final int oneTooBig = RSA_2048_modulus.bitLength() - 10;
    for (int i = 0; i < oneTooBig; i++) {
        sig.update((byte) i);
    }
    try {
        sig.sign();
        fail("Should throw exception when data is too large");
    } catch (SignatureException expected) {
    }
}
Also used : PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) Signature(java.security.Signature) SignatureException(java.security.SignatureException) KeyFactory(java.security.KeyFactory)

Example 35 with RSAPrivateKeySpec

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

the class CipherTest method testRSA_ECB_NoPadding_Private_TooSmall_Success.

private void testRSA_ECB_NoPadding_Private_TooSmall_Success(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
    final PrivateKey privKey = kf.generatePrivate(keySpec);
    Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
    /*
         * You're actually encrypting with public keys, but there is no
         * distinction made here. It's all keyed off of what kind of key you're
         * using. ENCRYPT_MODE and DECRYPT_MODE are the same.
         */
    c.init(Cipher.ENCRYPT_MODE, privKey);
    byte[] encrypted = c.doFinal(RSA_Vector1_ZeroPadded_Encrypted);
    assertEncryptedEqualsNoPadding(provider, Cipher.ENCRYPT_MODE, TooShort_Vector_Zero_Padded, encrypted);
    c.init(Cipher.DECRYPT_MODE, privKey);
    encrypted = c.doFinal(RSA_Vector1_ZeroPadded_Encrypted);
    assertEncryptedEqualsNoPadding(provider, Cipher.DECRYPT_MODE, TooShort_Vector_Zero_Padded, encrypted);
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Aggregations

RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)52 KeyFactory (java.security.KeyFactory)31 PrivateKey (java.security.PrivateKey)28 RSAPrivateKey (java.security.interfaces.RSAPrivateKey)23 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)17 PublicKey (java.security.PublicKey)16 BigInteger (java.math.BigInteger)11 Signature (java.security.Signature)10 Cipher (javax.crypto.Cipher)10 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 IOException (java.io.IOException)4 KeyPairGenerator (java.security.KeyPairGenerator)4 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)4