Search in sources :

Example 71 with RSAPublicKeySpec

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

the class CipherTest method getDecryptKey.

private static synchronized Key getDecryptKey(String algorithm) throws Exception {
    Key key = DECRYPT_KEYS.get(algorithm);
    if (key != null) {
        return key;
    }
    if (algorithm.startsWith("RSA")) {
        KeyFactory kf = KeyFactory.getInstance("RSA");
        RSAPublicKeySpec keySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
        key = kf.generatePublic(keySpec);
    } else {
        assertFalse(algorithm, isAsymmetric(algorithm));
        key = getEncryptKey(algorithm);
    }
    DECRYPT_KEYS.put(algorithm, key);
    return key;
}
Also used : RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 72 with RSAPublicKeySpec

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

the class CipherTest method testRSA_ECB_NoPadding_GetBlockSize_Success.

private void testRSA_ECB_NoPadding_GetBlockSize_Success(String provider) throws Exception {
    Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
    if (StandardNames.IS_RI) {
        assertEquals(0, c.getBlockSize());
    } else {
        try {
            c.getBlockSize();
            fail();
        } catch (IllegalStateException expected) {
        }
    }
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    final PublicKey pubKey = kf.generatePublic(pubKeySpec);
    c.init(Cipher.ENCRYPT_MODE, pubKey);
    assertEquals(getExpectedBlockSize("RSA", Cipher.ENCRYPT_MODE, provider), c.getBlockSize());
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) Cipher(javax.crypto.Cipher) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 73 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec 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)

Example 74 with RSAPublicKeySpec

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

the class RSAPublicKeyTest method test_getPublicExponent.

/**
     * java.security.interfaces.RSAPublicKey
     * #getPublicExponent()
     */
public void test_getPublicExponent() throws Exception {
    KeyFactory gen = KeyFactory.getInstance("RSA");
    final BigInteger n = BigInteger.valueOf(3233);
    final BigInteger e = BigInteger.valueOf(17);
    RSAPublicKey key = (RSAPublicKey) gen.generatePublic(new RSAPublicKeySpec(n, e));
    assertEquals("invalid public exponent", e, key.getPublicExponent());
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) BigInteger(java.math.BigInteger) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory)

Example 75 with RSAPublicKeySpec

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

the class ServerKeyExchange method getRSAPublicKey.

/**
     * Returns RSAPublicKey generated using ServerRSAParams
     * (rsa_modulus and rsa_exponent).
     *
     * @return
     */
public RSAPublicKey getRSAPublicKey() {
    if (key != null) {
        return key;
    }
    try {
        KeyFactory kf = KeyFactory.getInstance("RSA");
        key = (RSAPublicKey) kf.generatePublic(new RSAPublicKeySpec(par1, par2));
    } catch (Exception e) {
        return null;
    }
    return key;
}
Also used : RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) KeyFactory(java.security.KeyFactory) IOException(java.io.IOException)

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