Search in sources :

Example 56 with RSAPublicKeySpec

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

the class CipherTest method testRSA_ECB_NoPadding_Public_OnlyDoFinal_Success.

private void testRSA_ECB_NoPadding_Public_OnlyDoFinal_Success(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec keySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    final PublicKey privKey = kf.generatePublic(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_Encrypt_Private);
    assertEncryptedEqualsNoPadding(provider, Cipher.ENCRYPT_MODE, RSA_2048_Vector1, encrypted);
    c.init(Cipher.DECRYPT_MODE, privKey);
    encrypted = c.doFinal(RSA_Vector1_Encrypt_Private);
    assertEncryptedEqualsNoPadding(provider, Cipher.DECRYPT_MODE, RSA_2048_Vector1, encrypted);
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 57 with RSAPublicKeySpec

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

the class CipherTest method testRSA_ECB_NoPadding_GetIV_Success.

private void testRSA_ECB_NoPadding_GetIV_Success(String provider) throws Exception {
    KeyFactory kf = KeyFactory.getInstance("RSA");
    RSAPublicKeySpec pubKeySpec = new RSAPublicKeySpec(RSA_2048_modulus, RSA_2048_publicExponent);
    final PublicKey pubKey = kf.generatePublic(pubKeySpec);
    Cipher c = Cipher.getInstance("RSA/ECB/NoPadding", provider);
    assertNull("ECB mode has no IV and should be null", c.getIV());
    c.init(Cipher.ENCRYPT_MODE, pubKey);
    assertNull("ECB mode has no IV and should be null", c.getIV());
}
Also used : RSAPublicKey(java.security.interfaces.RSAPublicKey) PublicKey(java.security.PublicKey) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 58 with RSAPublicKeySpec

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

the class RSAPublicKeySpecTest method testGetModulus.

/**
     * Test for <code>getModulus()</code> method<br>
     * Assertion: returns modulus
     */
public final void testGetModulus() {
    RSAPublicKeySpec rpks = new RSAPublicKeySpec(BigInteger.valueOf(1234567890L), BigInteger.valueOf(3L));
    assertTrue(BigInteger.valueOf(1234567890L).equals(rpks.getModulus()));
}
Also used : RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 59 with RSAPublicKeySpec

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

the class RSAPublicKeySpecTest method testRSAPublicKeySpec01.

/**
     * Test #1 for <code>RSAPublicKeySpec</code> constructor
     * Assertion: Constructs <code>RSAPublicKeySpec</code>
     * object using valid parameters
     */
public final void testRSAPublicKeySpec01() {
    KeySpec ks = new RSAPublicKeySpec(BigInteger.valueOf(1234567890L), BigInteger.valueOf(3L));
    assertTrue(ks instanceof RSAPublicKeySpec);
}
Also used : KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 60 with RSAPublicKeySpec

use of java.security.spec.RSAPublicKeySpec in project XobotOS by xamarin.

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