Search in sources :

Example 41 with RSAPrivateKeySpec

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

the class RSAPrivateKeySpecTest method testGetPrivateExponent.

/**
     * Test for <code>getPrivateExponent()</code> method<br>
     * Assertion: returns private exponent
     */
public final void testGetPrivateExponent() {
    RSAPrivateKeySpec rpks = new RSAPrivateKeySpec(BigInteger.valueOf(1234567890L), BigInteger.valueOf(3L));
    assertEquals(3L, rpks.getPrivateExponent().longValue());
}
Also used : RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec)

Example 42 with RSAPrivateKeySpec

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

the class SignatureTest method testSign_NONEwithRSA_Key_DataTooLarge_SingleByte_Failure.

public void testSign_NONEwithRSA_Key_DataTooLarge_SingleByte_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);
    // This should make it two bytes too big.
    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 43 with RSAPrivateKeySpec

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

the class CipherTest method testRSA_ECB_NoPadding_Private_OnlyDoFinal_Success.

private void testRSA_ECB_NoPadding_Private_OnlyDoFinal_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 decrypting with private 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_2048_Vector1);
    assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, encrypted));
    c.init(Cipher.DECRYPT_MODE, privKey);
    encrypted = c.doFinal(RSA_2048_Vector1);
    assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, 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)

Example 44 with RSAPrivateKeySpec

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

the class CipherTest method testRSA_ECB_NoPadding_Private_SingleByteUpdateThenEmptyDoFinal_Success.

private void testRSA_ECB_NoPadding_Private_SingleByteUpdateThenEmptyDoFinal_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 decrypting with private 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);
    int i;
    for (i = 0; i < RSA_2048_Vector1.length / 2; i++) {
        c.update(RSA_2048_Vector1, i, 1);
    }
    byte[] encrypted = c.doFinal(RSA_2048_Vector1, i, RSA_2048_Vector1.length - i);
    assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, encrypted));
    c.init(Cipher.DECRYPT_MODE, privKey);
    for (i = 0; i < RSA_2048_Vector1.length / 2; i++) {
        c.update(RSA_2048_Vector1, i, 1);
    }
    encrypted = c.doFinal(RSA_2048_Vector1, i, RSA_2048_Vector1.length - i);
    assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, 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)

Example 45 with RSAPrivateKeySpec

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

the class RSAKeyTest method test_getModulus.

/**
     * java.security.interfaces.RSAKey
     * #getModulus()
     * test covers following use cases
     *   Case 1: check private key
     *   Case 2: check public key
     */
public void test_getModulus() throws Exception {
    KeyFactory gen = KeyFactory.getInstance("RSA");
    final BigInteger n = BigInteger.valueOf(3233);
    final BigInteger d = BigInteger.valueOf(2753);
    final BigInteger e = BigInteger.valueOf(17);
    RSAKey key = null;
    // Case 1: check private key
    key = (RSAKey) gen.generatePrivate(new RSAPrivateKeySpec(n, d));
    assertEquals("invalid modulus", n, key.getModulus());
    // Case 2: check public key
    key = (RSAKey) gen.generatePublic(new RSAPublicKeySpec(n, e));
    assertEquals("invalid modulus", n, key.getModulus());
}
Also used : RSAKey(java.security.interfaces.RSAKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) 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