Search in sources :

Example 16 with RSAPrivateKeySpec

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

the class CipherTest method testRSA_ECB_NoPadding_Private_OnlyDoFinalWithOffset_Success.

private void testRSA_ECB_NoPadding_Private_OnlyDoFinalWithOffset_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 = new byte[RSA_Vector1_Encrypt_Private.length];
    final int encryptLen = c.doFinal(RSA_2048_Vector1, 0, RSA_2048_Vector1.length, encrypted, 0);
    assertEquals("Encrypted size should match expected", RSA_Vector1_Encrypt_Private.length, encryptLen);
    assertTrue("Encrypted should match expected", Arrays.equals(RSA_Vector1_Encrypt_Private, encrypted));
    c.init(Cipher.DECRYPT_MODE, privKey);
    final int decryptLen = c.doFinal(RSA_2048_Vector1, 0, RSA_2048_Vector1.length, encrypted, 0);
    assertEquals("Encrypted size should match expected", RSA_Vector1_Encrypt_Private.length, decryptLen);
    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 17 with RSAPrivateKeySpec

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

the class CipherTest method testRSA_ECB_NoPadding_Private_UpdateInAndOutPlusDoFinal_TooBig_Failure.

private void testRSA_ECB_NoPadding_Private_UpdateInAndOutPlusDoFinal_TooBig_Failure(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[] output = new byte[RSA_2048_Vector1.length];
    c.update(RSA_Vector1_ZeroPadded_Encrypted, 0, RSA_Vector1_ZeroPadded_Encrypted.length, output);
    try {
        c.doFinal(RSA_Vector1_ZeroPadded_Encrypted);
        fail("Should have error when block size is too big.");
    } catch (IllegalBlockSizeException success) {
        assertFalse(provider, "BC".equals(provider));
    } catch (ArrayIndexOutOfBoundsException success) {
        assertEquals("BC", provider);
    }
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 18 with RSAPrivateKeySpec

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

the class CipherTest method testRSA_ECB_NoPadding_Private_OnlyDoFinal_TooBig_Failure.

private void testRSA_ECB_NoPadding_Private_OnlyDoFinal_TooBig_Failure(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[] tooBig_Vector = new byte[RSA_Vector1_ZeroPadded_Encrypted.length * 2];
    System.arraycopy(RSA_Vector1_ZeroPadded_Encrypted, 0, tooBig_Vector, 0, RSA_Vector1_ZeroPadded_Encrypted.length);
    System.arraycopy(RSA_Vector1_ZeroPadded_Encrypted, 0, tooBig_Vector, RSA_Vector1_ZeroPadded_Encrypted.length, RSA_Vector1_ZeroPadded_Encrypted.length);
    try {
        c.doFinal(tooBig_Vector);
        fail("Should have error when block size is too big.");
    } catch (IllegalBlockSizeException success) {
        assertFalse(provider, "BC".equals(provider));
    } catch (ArrayIndexOutOfBoundsException success) {
        assertEquals("BC", provider);
    }
}
Also used : RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PrivateKey(java.security.PrivateKey) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 19 with RSAPrivateKeySpec

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

the class CipherTest method getEncryptKey.

private static synchronized Key getEncryptKey(String algorithm) throws Exception {
    Key key = ENCRYPT_KEYS.get(algorithm);
    if (key != null) {
        return key;
    }
    if (algorithm.startsWith("RSA")) {
        KeyFactory kf = KeyFactory.getInstance("RSA");
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
        key = kf.generatePrivate(keySpec);
    } else if (isPBE(algorithm)) {
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
        key = skf.generateSecret(new PBEKeySpec("secret".toCharArray()));
    } else {
        KeyGenerator kg = KeyGenerator.getInstance(getBaseAlgorithm(algorithm));
        key = kg.generateKey();
    }
    ENCRYPT_KEYS.put(algorithm, key);
    return key;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyGenerator(javax.crypto.KeyGenerator) 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 20 with RSAPrivateKeySpec

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

the class RSAPrivateKeyTest method test_getPrivateExponent.

/**
     * java.security.interfaces.RSAPrivateKey
     * #getPrivateExponent()
     */
public void test_getPrivateExponent() throws Exception {
    KeyFactory gen = KeyFactory.getInstance("RSA");
    final BigInteger n = BigInteger.valueOf(3233);
    final BigInteger d = BigInteger.valueOf(2753);
    RSAPrivateKey key = (RSAPrivateKey) gen.generatePrivate(new RSAPrivateKeySpec(n, d));
    assertEquals("invalid private exponent", d, key.getPrivateExponent());
}
Also used : RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) 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