Search in sources :

Example 16 with AlgorithmParameterSpec

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

the class CipherTest method test_initWithKeyAlgorithmParameters.

public void test_initWithKeyAlgorithmParameters() throws Exception {
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    assertNotNull(c.getParameters());
    try {
        c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_3DES, ap);
        fail();
    } catch (InvalidKeyException expected) {
    }
    try {
        c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, (AlgorithmParameters) null);
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 17 with AlgorithmParameterSpec

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

the class CipherTest method test_initWithAlgorithmParameterSpec.

/**
     * javax.crypto.Cipher#init(int, java.security.Key,
     *        java.security.spec.AlgorithmParameterSpec)
     */
public void test_initWithAlgorithmParameterSpec() throws Exception {
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher cipher = Cipher.getInstance(ALGORITHM_3DES + "/CBC/PKCS5Padding");
    cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap);
    byte[] cipherIV = cipher.getIV();
    assertTrue("IVs differ", Arrays.equals(cipherIV, IV));
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_3DES, ap);
        fail();
    } catch (InvalidKeyException expected) {
    }
    cipher = Cipher.getInstance("DES/CBC/NoPadding");
    ap = new RSAKeyGenParameterSpec(10, new BigInteger("10"));
    try {
        cipher.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
        fail();
    } catch (InvalidAlgorithmParameterException expected) {
    }
}
Also used : InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) RSAKeyGenParameterSpec(java.security.spec.RSAKeyGenParameterSpec) BigInteger(java.math.BigInteger) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) InvalidKeyException(java.security.InvalidKeyException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec)

Example 18 with AlgorithmParameterSpec

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

the class AlgorithmParameterGeneratorSpiTest method testAlgorithmParameterGeneratorSpi01.

/**
     * Test for <code>AlgorithmParameterGeneratorSpi</code> constructor
     * Assertion: constructs AlgorithmParameterGeneratorSpi
     */
public void testAlgorithmParameterGeneratorSpi01() throws InvalidAlgorithmParameterException {
    MyAlgorithmParameterGeneratorSpi algParGen = new MyAlgorithmParameterGeneratorSpi();
    AlgorithmParameters param = algParGen.engineGenerateParameters();
    assertNull("Not null parameters", param);
    AlgorithmParameterSpec pp = null;
    algParGen.engineInit(pp, new SecureRandom());
    try {
        algParGen.engineInit(pp, null);
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    algParGen.engineInit(0, null);
    algParGen.engineInit(0, new SecureRandom());
    try {
        algParGen.engineInit(-10, null);
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
    try {
        algParGen.engineInit(-10, new SecureRandom());
        fail("IllegalArgumentException must be thrown");
    } catch (IllegalArgumentException e) {
    }
}
Also used : MyAlgorithmParameterGeneratorSpi(org.apache.harmony.security.tests.support.MyAlgorithmParameterGeneratorSpi) SecureRandom(java.security.SecureRandom) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) AlgorithmParameters(java.security.AlgorithmParameters)

Example 19 with AlgorithmParameterSpec

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

the class OAEPParameterSpecTest method testOAEPParameterSpec.

/**
     * OAEPParameterSpec(String mdName, String mgfName, AlgorithmParameterSpec
     * mgfSpec, PSource pSrc) method testing. Tests that NullPointerException
     * is thrown in the case of inappropriate constructor parameters and checks
     * the value of DEFAULT field.
     */
public void testOAEPParameterSpec() {
    // using init values for OAEPParameterSpec.DEFAULT
    String mdName = "SHA-1";
    String mgfName = "MGF1";
    AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
    PSource pSrc = PSource.PSpecified.DEFAULT;
    try {
        new OAEPParameterSpec(null, mgfName, mgfSpec, pSrc);
        fail("NullPointerException should be thrown in the case of " + "null mdName.");
    } catch (NullPointerException e) {
    }
    try {
        new OAEPParameterSpec(mdName, null, mgfSpec, pSrc);
        fail("NullPointerException should be thrown in the case of " + "null mgfName.");
    } catch (NullPointerException e) {
    }
    try {
        new OAEPParameterSpec(mdName, mgfName, mgfSpec, null);
        fail("NullPointerException should be thrown in the case of " + "null pSrc.");
    } catch (NullPointerException e) {
    }
    assertTrue("The message digest algorithm name of " + "OAEPParameterSpec.DEFAULT field should be " + mdName, OAEPParameterSpec.DEFAULT.getDigestAlgorithm().equals(mdName));
    assertTrue("The mask generation function algorithm name of " + "OAEPParameterSpec.DEFAULT field should be " + mgfName, OAEPParameterSpec.DEFAULT.getMGFAlgorithm().equals(mgfName));
    assertTrue("The mask generation function parameters of " + "OAEPParameterSpec.DEFAULT field should be the same object " + "as MGF1ParameterSpec.SHA1", OAEPParameterSpec.DEFAULT.getMGFParameters() == MGF1ParameterSpec.SHA1);
    assertTrue("The source of the encoding input P of " + "OAEPParameterSpec.DEFAULT field should be the same object " + "PSource.PSpecified.DEFAULT", OAEPParameterSpec.DEFAULT.getPSource() == PSource.PSpecified.DEFAULT);
}
Also used : PSource(javax.crypto.spec.PSource) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec)

Example 20 with AlgorithmParameterSpec

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

the class OAEPParameterSpecTest method testGetMGFAlgorithm.

/**
     * getMGFAlgorithm() method testing.
     */
public void testGetMGFAlgorithm() {
    String mdName = "SHA-1";
    String mgfName = "MGF1";
    AlgorithmParameterSpec mgfSpec = MGF1ParameterSpec.SHA1;
    PSource pSrc = PSource.PSpecified.DEFAULT;
    OAEPParameterSpec ps = new OAEPParameterSpec(mdName, mgfName, mgfSpec, pSrc);
    assertTrue("The returned value does not equal to the " + "value specified in the constructor.", ps.getMGFAlgorithm().equals(mgfName));
}
Also used : PSource(javax.crypto.spec.PSource) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) OAEPParameterSpec(javax.crypto.spec.OAEPParameterSpec)

Aggregations

AlgorithmParameterSpec (java.security.spec.AlgorithmParameterSpec)186 IvParameterSpec (javax.crypto.spec.IvParameterSpec)59 Cipher (javax.crypto.Cipher)55 InvalidAlgorithmParameterException (java.security.InvalidAlgorithmParameterException)51 InvalidKeyException (java.security.InvalidKeyException)42 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)37 SecureRandom (java.security.SecureRandom)27 SecretKey (javax.crypto.SecretKey)27 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)24 BigInteger (java.math.BigInteger)21 BadPaddingException (javax.crypto.BadPaddingException)21 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)20 RSAKeyGenParameterSpec (java.security.spec.RSAKeyGenParameterSpec)19 ShortBufferException (javax.crypto.ShortBufferException)19 Key (java.security.Key)18 SecretKeySpec (javax.crypto.spec.SecretKeySpec)18 AlgorithmParameters (java.security.AlgorithmParameters)17 KeyGenerator (javax.crypto.KeyGenerator)17 OAEPParameterSpec (javax.crypto.spec.OAEPParameterSpec)15 IOException (java.io.IOException)14