Search in sources :

Example 71 with SecureRandom

use of java.security.SecureRandom in project robovm by robovm.

the class KeyAgreementTest method test_initLjava_security_KeyLjava_security_SecureRandom.

public void test_initLjava_security_KeyLjava_security_SecureRandom() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    createKeys();
    KeyAgreement[] kAgs = createKAs();
    KeyAgreement ka = KeyAgreement.getInstance("DH");
    ka.init(privKey, new SecureRandom());
    try {
        ka.init(publKey, new SecureRandom());
        fail("InvalidKeyException expected");
    } catch (InvalidKeyException e) {
    //expected
    }
}
Also used : SecureRandom(java.security.SecureRandom) KeyAgreement(javax.crypto.KeyAgreement) InvalidKeyException(java.security.InvalidKeyException)

Example 72 with SecureRandom

use of java.security.SecureRandom in project robovm by robovm.

the class APSpecSpi method testKeyGeneratorSpi01.

/**
     * Test for <code>KeyGeneratorSpi</code> constructor Assertion: constructs
     * KeyGeneratorSpi
     */
public void testKeyGeneratorSpi01() throws InvalidAlgorithmParameterException {
    Mock_KeyGeneratorSpi kgSpi = new Mock_KeyGeneratorSpi();
    assertNull("Not null result", kgSpi.engineGenerateKey());
    try {
        kgSpi.engineInit(77, new SecureRandom());
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    try {
        kgSpi.engineInit(new SecureRandom());
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    AlgorithmParameterSpec aps = null;
    try {
        kgSpi.engineInit(aps, new SecureRandom());
        fail("InvalidAlgorithmParameterException must be thrown when parameter is null");
    } catch (InvalidAlgorithmParameterException e) {
    }
    aps = new APSpecSpi();
    kgSpi.engineInit(aps, new SecureRandom());
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 73 with SecureRandom

use of java.security.SecureRandom in project robovm by robovm.

the class myKeyGenerator method testInitParams.

/*
     * Test for <code>init(AlgorithmParameterSpec params)</code> and
     * <code>init(AlgorithmParameterSpec params, SecureRandom random)</code> methods
     * Assertion: throws InvalidAlgorithmParameterException when params is null
     */
public void testInitParams() throws Exception {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    KeyGenerator[] kgs = createKGs();
    AlgorithmParameterSpec aps = null;
    for (int i = 0; i < kgs.length; i++) {
        try {
            kgs[i].init(aps);
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        }
        try {
            kgs[i].init(aps, new SecureRandom());
            fail("InvalidAlgorithmParameterException must be thrown");
        } catch (InvalidAlgorithmParameterException e) {
        }
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) SecureRandom(java.security.SecureRandom) KeyGenerator(javax.crypto.KeyGenerator) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 74 with SecureRandom

use of java.security.SecureRandom in project robovm by robovm.

the class CipherOutputStream1Test method test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher.

public void test_ConstructorLjava_io_OutputStreamLjavax_crypto_Cipher() throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    KeyGenerator kg = KeyGenerator.getInstance("DES");
    kg.init(56, new SecureRandom());
    Key key = kg.generateKey();
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, key);
    CipherOutputStream cos = new CipherOutputStream(baos, c);
    assertNotNull(cos);
}
Also used : CipherOutputStream(javax.crypto.CipherOutputStream) SecureRandom(java.security.SecureRandom) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Cipher(javax.crypto.Cipher) NullCipher(javax.crypto.NullCipher) KeyGenerator(javax.crypto.KeyGenerator) Key(java.security.Key)

Example 75 with SecureRandom

use of java.security.SecureRandom in project robovm by robovm.

the class CipherTest method test_getOutputSizeI.

/**
     * javax.crypto.Cipher#getOutputSize(int)
     */
public void test_getOutputSizeI() throws Exception {
    Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/ECB/PKCS5Padding");
    try {
        cipher.getOutputSize(25);
        fail();
    } catch (IllegalStateException expected) {
    }
    cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, new SecureRandom());
    // A 25-byte input could result in at least 4 8-byte blocks
    int result = cipher.getOutputSize(25);
    assertTrue("Output size too small", result > 31);
    // A 8-byte input should result in 2 8-byte blocks
    result = cipher.getOutputSize(8);
    assertTrue("Output size too small", result > 15);
}
Also used : SecureRandom(java.security.SecureRandom) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher)

Aggregations

SecureRandom (java.security.SecureRandom)720 SSLContext (javax.net.ssl.SSLContext)106 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)97 IOException (java.io.IOException)87 Test (org.junit.Test)76 SecretKey (javax.crypto.SecretKey)62 X509Certificate (java.security.cert.X509Certificate)61 KeyGenerator (javax.crypto.KeyGenerator)57 TrustManager (javax.net.ssl.TrustManager)56 X509TrustManager (javax.net.ssl.X509TrustManager)47 Cipher (javax.crypto.Cipher)46 KeyPairGenerator (java.security.KeyPairGenerator)44 BigInteger (java.math.BigInteger)42 CertificateException (java.security.cert.CertificateException)40 InvalidKeyException (java.security.InvalidKeyException)35 KeyPair (java.security.KeyPair)34 KeyStore (java.security.KeyStore)34 SecretKeySpec (javax.crypto.spec.SecretKeySpec)30 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)28 KeyManagementException (java.security.KeyManagementException)28