Search in sources :

Example 16 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project robovm by robovm.

the class mySecretKeyFactory method test_getAlgorithm.

public void test_getAlgorithm() throws NoSuchAlgorithmException {
    for (int i = 0; i < validValues.length; i++) {
        SecretKeyFactory secKF = SecretKeyFactory.getInstance(validValues[i]);
        assertEquals("Incorrect algorithm", secKF.getAlgorithm(), validValues[i]);
    }
    Mock_SecretKeyFactory msf = new Mock_SecretKeyFactory(null, null, null);
    assertNull(msf.getAlgorithm());
}
Also used : SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 17 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project robovm by robovm.

the class mySecretKeyFactory method test_getProvider.

public void test_getProvider() throws NoSuchAlgorithmException {
    for (int i = 0; i < validValues.length; i++) {
        SecretKeyFactory secKF = SecretKeyFactory.getInstance(validValues[i]);
        assertNotNull(secKF.getProvider());
    }
    Mock_SecretKeyFactory msf = new Mock_SecretKeyFactory(null, null, null);
    assertNull(msf.getProvider());
}
Also used : SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 18 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project robovm by robovm.

the class mySecretKeyFactory method testSecretKeyFactory06.

/**
     * Test for <code>getInstance(String algorithm, String provider)</code>
     * method
     * Assertion: returns SecretKeyFactory object
     */
public void testSecretKeyFactory06() throws NoSuchProviderException, NoSuchAlgorithmException {
    if (!DEFSupported) {
        fail(NotSupportMsg);
        return;
    }
    for (int i = 0; i < validValues.length; i++) {
        SecretKeyFactory secKF = SecretKeyFactory.getInstance(validValues[i], defaultProviderName);
        assertEquals("Incorrect algorithm", secKF.getAlgorithm(), validValues[i]);
        assertEquals("Incorrect provider", secKF.getProvider().getName(), defaultProviderName);
    }
}
Also used : SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 19 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project robovm by robovm.

the class CipherPBEThread method crypt.

@Override
public void crypt() throws Exception {
    byte[] output = new byte[128];
    byte[] decrypted = new byte[128];
    byte[] input = getData().getBytes();
    byte[] salt = new byte[8];
    SecureRandom sr = new SecureRandom();
    PBEKeySpec keySpec = new PBEKeySpec("top sicret password".toCharArray());
    SecretKeyFactory skf = SecretKeyFactory.getInstance(getAlgName());
    SecretKey key = skf.generateSecret(keySpec);
    Cipher cip = Cipher.getInstance(getAlgName() + "/" + getMode() + "/" + getPadding());
    sr.nextBytes(salt);
    PBEParameterSpec parSpec = new PBEParameterSpec(salt, getKeyLength());
    cip.init(Cipher.ENCRYPT_MODE, key, parSpec);
    cip.doFinal(input, 0, input.length, output);
    int outputSize = cip.getOutputSize(input.length);
    cip.init(Cipher.DECRYPT_MODE, key, parSpec);
    cip.doFinal(output, 0, outputSize, decrypted);
    checkEncodedData(getData().getBytes(), decrypted);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) SecureRandom(java.security.SecureRandom) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 20 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory 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)

Aggregations

SecretKeyFactory (javax.crypto.SecretKeyFactory)129 SecretKey (javax.crypto.SecretKey)84 PBEKeySpec (javax.crypto.spec.PBEKeySpec)75 Cipher (javax.crypto.Cipher)58 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 DESKeySpec (javax.crypto.spec.DESKeySpec)28 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)26 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)26 KeySpec (java.security.spec.KeySpec)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)23 SecureRandom (java.security.SecureRandom)18 KeyStoreException (java.security.KeyStoreException)16 IOException (java.io.IOException)15 InvalidKeyException (java.security.InvalidKeyException)14 PrivateKey (java.security.PrivateKey)12 CertificateException (java.security.cert.CertificateException)12 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)12 UnrecoverableKeyException (java.security.UnrecoverableKeyException)11 Key (java.security.Key)10 KeyFactory (java.security.KeyFactory)10