Search in sources :

Example 16 with KeySpec

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

the class DSAPublicKeySpecTest method testDSAPublicKeySpec.

/**
     * Test for <code>DSAPublicKeySpec</code> ctor
     */
public final void testDSAPublicKeySpec() {
    KeySpec ks = new DSAPublicKeySpec(// y
    new BigInteger("1"), // p
    new BigInteger("2"), // q
    new BigInteger("3"), // g
    new BigInteger("4"));
    assertTrue(ks instanceof DSAPublicKeySpec);
}
Also used : DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) KeySpec(java.security.spec.KeySpec) BigInteger(java.math.BigInteger) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec)

Example 17 with KeySpec

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

the class SecretKeyFactorySpiTest method testSecretKeyFactorySpi01.

/**
     * Test for <code>SecretKeyFactorySpi</code> constructor Assertion:
     * constructs SecretKeyFactorySpi
     */
public void testSecretKeyFactorySpi01() throws InvalidKeyException, InvalidKeySpecException {
    Mock_SecretKeyFactorySpi skfSpi = new Mock_SecretKeyFactorySpi();
    SecretKey sk = null;
    assertNull("Not null result", skfSpi.engineTranslateKey(sk));
    KeySpec kspec = null;
    assertNull("Not null result", skfSpi.engineGenerateSecret(kspec));
    assertNull("Not null result", skfSpi.engineGetKeySpec(sk, null));
}
Also used : SecretKey(javax.crypto.SecretKey) KeySpec(java.security.spec.KeySpec)

Example 18 with KeySpec

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

the class mySecretKeyFactory method testSecretKeyFactory10.

/**
     * Test for <code>generateSecret(KeySpec keySpec)</code> and
     * <code>getKeySpec(SecretKey key, Class keySpec)
     * methods
     * Assertion:
     * throw InvalidKeySpecException if parameter is inappropriate
     */
public void testSecretKeyFactory10() throws InvalidKeyException, InvalidKeySpecException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    byte[] bb = new byte[24];
    KeySpec ks = (defaultAlgorithm.equals(defaultAlgorithm2) ? (KeySpec) new DESKeySpec(bb) : (KeySpec) new DESedeKeySpec(bb));
    KeySpec rks = null;
    SecretKeySpec secKeySpec = new SecretKeySpec(bb, defaultAlgorithm);
    SecretKey secKey = null;
    SecretKeyFactory[] skF = createSKFac();
    assertNotNull("SecretKeyFactory object were not created", skF);
    for (int i = 0; i < skF.length; i++) {
        try {
            skF[i].generateSecret(null);
            fail("generateSecret(null): InvalidKeySpecException must be thrown");
        } catch (InvalidKeySpecException e) {
        }
        secKey = skF[i].generateSecret(ks);
        try {
            skF[i].getKeySpec(null, null);
            fail("getKeySpec(null,null): InvalidKeySpecException must be thrown");
        } catch (InvalidKeySpecException e) {
        }
        try {
            skF[i].getKeySpec(null, ks.getClass());
            fail("getKeySpec(null, Class): InvalidKeySpecException must be thrown");
        } catch (InvalidKeySpecException e) {
        }
        try {
            skF[i].getKeySpec(secKey, null);
            fail("getKeySpec(secKey, null): NullPointerException or InvalidKeySpecException must be thrown");
        } catch (InvalidKeySpecException e) {
        // Expected
        } catch (NullPointerException e) {
        // Expected
        }
        try {
            Class c;
            if (defaultAlgorithm.equals(defaultAlgorithm2)) {
                c = DESedeKeySpec.class;
            } else {
                c = DESKeySpec.class;
            }
            skF[i].getKeySpec(secKeySpec, c);
            fail("getKeySpec(secKey, Class): InvalidKeySpecException must be thrown");
        } catch (InvalidKeySpecException e) {
        }
        rks = skF[i].getKeySpec(secKeySpec, ks.getClass());
        if (defaultAlgorithm.equals(defaultAlgorithm1)) {
            assertTrue("Incorrect getKeySpec() result 1", rks instanceof DESedeKeySpec);
        } else {
            assertTrue("Incorrect getKeySpec() result 1", rks instanceof DESKeySpec);
        }
        rks = skF[i].getKeySpec(secKey, ks.getClass());
        if (defaultAlgorithm.equals(defaultAlgorithm1)) {
            assertTrue("Incorrect getKeySpec() result 2", rks instanceof DESedeKeySpec);
        } else {
            assertTrue("Incorrect getKeySpec() result 2", rks instanceof DESKeySpec);
        }
    }
}
Also used : SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) KeySpec(java.security.spec.KeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 19 with KeySpec

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

the class SecretKeyFactoryThread method test.

@Override
public void test() throws Exception {
    SecretKeyFactory skf = SecretKeyFactory.getInstance(algName);
    byte[] b = new byte[24];
    KeySpec ks = (KeySpec) ((algName == "DES") ? new DESKeySpec(b) : (algName == "DESede") ? new DESedeKeySpec(b) : new PBEKeySpec("passw".toCharArray()));
    skf.generateSecret(ks);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 20 with KeySpec

use of java.security.spec.KeySpec in project syncany by syncany.

the class CipherUtil method createMasterKey.

public static SaltedSecretKey createMasterKey(String password, byte[] salt) throws CipherException {
    try {
        logger.log(Level.FINE, "- Creating secret key using {0} with {1} rounds, key size {2} bit ...", new Object[] { MASTER_KEY_DERIVATION_FUNCTION, MASTER_KEY_DERIVATION_ROUNDS, MASTER_KEY_SIZE });
        SecretKeyFactory factory = SecretKeyFactory.getInstance(MASTER_KEY_DERIVATION_FUNCTION);
        KeySpec pbeKeySpec = new PBEKeySpec(password.toCharArray(), salt, MASTER_KEY_DERIVATION_ROUNDS, MASTER_KEY_SIZE);
        SecretKey masterKey = factory.generateSecret(pbeKeySpec);
        return new SaltedSecretKey(masterKey, salt);
    } catch (Exception e) {
        throw new CipherException(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) OperatorCreationException(org.bouncycastle.operator.OperatorCreationException) SignatureException(java.security.SignatureException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchProviderException(java.security.NoSuchProviderException)

Aggregations

KeySpec (java.security.spec.KeySpec)149 PBEKeySpec (javax.crypto.spec.PBEKeySpec)66 SecretKeyFactory (javax.crypto.SecretKeyFactory)62 KeyFactory (java.security.KeyFactory)46 SecretKeySpec (javax.crypto.spec.SecretKeySpec)46 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)39 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 SecretKey (javax.crypto.SecretKey)35 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)34 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)28 BigInteger (java.math.BigInteger)25 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)25 DSAPublicKeySpec (java.security.spec.DSAPublicKeySpec)23 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)21 PrivateKey (java.security.PrivateKey)19 Cipher (javax.crypto.Cipher)16 IOException (java.io.IOException)15 DSAPrivateKeySpec (java.security.spec.DSAPrivateKeySpec)14 PublicKey (java.security.PublicKey)13 InvalidKeyException (java.security.InvalidKeyException)12