Search in sources :

Example 1 with DESedeKeySpec

use of javax.crypto.spec.DESedeKeySpec 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 2 with DESedeKeySpec

use of javax.crypto.spec.DESedeKeySpec in project robovm by robovm.

the class DESedeKeySpecTest method testGetKey.

/**
     * getKey() method testing. Checks that modification of returned key
     * does not affect the internal key. Also test check an equality of
     * the key with the key specified in the constructor. The object under
     * the test is created by different constructors.
     */
public void testGetKey() {
    byte[] key = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2 };
    DESedeKeySpec ks;
    try {
        ks = new DESedeKeySpec(key);
    } catch (InvalidKeyException e) {
        fail("InvalidKeyException should not be thrown.");
        return;
    }
    byte[] res = ks.getKey();
    assertTrue("The returned array should be equal to the specified " + "in constructor.", Arrays.equals(key, res));
    res[0] += 1;
    assertFalse("The modification of returned key should not affect" + "the underlying key.", key[0] == res[0]);
    byte[] key1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3 };
    try {
        ks = new DESedeKeySpec(key1, 2);
    } catch (InvalidKeyException e) {
        fail("InvalidKeyException should not be thrown.");
        return;
    }
    res = ks.getKey();
    assertNotSame("The returned array should not be the same object " + "as specified in a constructor.", key1, res);
    byte[] exp = new byte[24];
    System.arraycopy(key1, 2, exp, 0, 24);
    assertTrue("The returned array should be equal to the specified " + "in constructor.", Arrays.equals(exp, res));
}
Also used : DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) InvalidKeyException(java.security.InvalidKeyException)

Example 3 with DESedeKeySpec

use of javax.crypto.spec.DESedeKeySpec 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 4 with DESedeKeySpec

use of javax.crypto.spec.DESedeKeySpec in project camel by apache.

the class XMLSecurityDataFormat method generateKeyEncryptionKey.

private Key generateKeyEncryptionKey(String algorithm) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
    DESedeKeySpec keySpec;
    Key secretKey;
    try {
        if (algorithm.equalsIgnoreCase("DESede")) {
            keySpec = new DESedeKeySpec(passPhrase);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
            secretKey = keyFactory.generateSecret(keySpec);
        } else if (algorithm.equalsIgnoreCase("SEED")) {
            secretKey = new SecretKeySpec(passPhrase, "SEED");
        } else if (algorithm.equalsIgnoreCase("CAMELLIA")) {
            secretKey = new SecretKeySpec(passPhrase, "CAMELLIA");
        } else {
            secretKey = new SecretKeySpec(passPhrase, "AES");
        }
        if (Arrays.equals(passPhrase, DEFAULT_KEY.getBytes())) {
            LOG.warn("Using the default encryption key is not secure");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeyException("InvalidKeyException due to invalid passPhrase: " + Arrays.toString(passPhrase));
    } catch (NoSuchAlgorithmException e) {
        throw new NoSuchAlgorithmException("NoSuchAlgorithmException while using algorithm: " + algorithm);
    } catch (InvalidKeySpecException e) {
        throw new InvalidKeySpecException("Invalid Key generated while using passPhrase: " + Arrays.toString(passPhrase));
    }
    return secretKey;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException) SecretKeyFactory(javax.crypto.SecretKeyFactory) PublicKey(java.security.PublicKey) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey)

Example 5 with DESedeKeySpec

use of javax.crypto.spec.DESedeKeySpec in project oxCore by GluuFederation.

the class StringEncrypter method decrypt.

/**
	 * Decrypt a string encrypted with this encrypter
	 * 
	 * @param encryptedString
	 *            Encrypted string
	 * @return Decrypted string
	 * @throws EncryptionException
	 */
public String decrypt(final String encryptedString, String encryptionKey) throws EncryptionException {
    lock.lock();
    try {
        final byte[] keyAsBytes = encryptionKey.getBytes(StringEncrypter.UNICODE_FORMAT);
        String encryptionScheme = StringEncrypter.DESEDE_ENCRYPTION_SCHEME;
        KeySpec keySpec;
        if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DESEDE_ENCRYPTION_SCHEME)) {
            keySpec = new DESedeKeySpec(keyAsBytes);
        } else if (encryptionScheme.equalsIgnoreCase(StringEncrypter.DES_ENCRYPTION_SCHEME)) {
            keySpec = new DESKeySpec(keyAsBytes);
        } else {
            throw new IllegalArgumentException("Encryption scheme not supported: " + encryptionScheme);
        }
        return decrypt(encryptedString, keySpec);
    } catch (final Exception e) {
        throw new EncryptionException(e);
    } finally {
        lock.unlock();
    }
}
Also used : DESKeySpec(javax.crypto.spec.DESKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) KeySpec(java.security.spec.KeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

DESedeKeySpec (javax.crypto.spec.DESedeKeySpec)11 SecretKeyFactory (javax.crypto.SecretKeyFactory)8 KeySpec (java.security.spec.KeySpec)6 DESKeySpec (javax.crypto.spec.DESKeySpec)6 InvalidKeyException (java.security.InvalidKeyException)4 SecretKeySpec (javax.crypto.spec.SecretKeySpec)4 Key (java.security.Key)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 Cipher (javax.crypto.Cipher)3 IvParameterSpec (javax.crypto.spec.IvParameterSpec)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 File (java.io.File)2 InputStream (java.io.InputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)2 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)2 SecretKey (javax.crypto.SecretKey)2 MyCipher (org.apache.harmony.crypto.tests.support.MyCipher)2 PrivateKey (java.security.PrivateKey)1 PublicKey (java.security.PublicKey)1