Search in sources :

Example 6 with DESedeKeySpec

use of javax.crypto.spec.DESedeKeySpec in project jdk8u_jdk by JetBrains.

the class DigestMD5Base method makeDesKeys.

/**
     * Create parity-adjusted keys suitable for DES / DESede encryption.
     *
     * @param input A non-null byte array containing key material for
     * DES / DESede.
     * @param desStrength A string specifying eithe a DES or a DESede key.
     * @return SecretKey An instance of either DESKeySpec or DESedeKeySpec.
     *
     * @throws NoSuchAlgorithmException if the either the DES or DESede
     * algorithms cannote be lodaed by JCE.
     * @throws InvalidKeyException if an invalid array of bytes is used
     * as a key for DES or DESede.
     * @throws InvalidKeySpecException in an invalid parameter is passed
     * to either te DESKeySpec of the DESedeKeySpec constructors.
     */
private static SecretKey makeDesKeys(byte[] input, String desStrength) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException {
    // Generate first subkey using first 7 bytes
    byte[] subkey1 = addDesParity(input, 0, 7);
    KeySpec spec = null;
    SecretKeyFactory desFactory = SecretKeyFactory.getInstance(desStrength);
    switch(desStrength) {
        case "des":
            spec = new DESKeySpec(subkey1, 0);
            if (logger.isLoggable(Level.FINEST)) {
                traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST42:DES key input: ", input);
                traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST43:DES key parity-adjusted: ", subkey1);
                traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST44:DES key material: ", ((DESKeySpec) spec).getKey());
                logger.log(Level.FINEST, "DIGEST45: is parity-adjusted? {0}", Boolean.valueOf(DESKeySpec.isParityAdjusted(subkey1, 0)));
            }
            break;
        case "desede":
            // Generate second subkey using second 7 bytes
            byte[] subkey2 = addDesParity(input, 7, 7);
            // Construct 24-byte encryption-decryption-encryption sequence
            byte[] ede = new byte[subkey1.length * 2 + subkey2.length];
            System.arraycopy(subkey1, 0, ede, 0, subkey1.length);
            System.arraycopy(subkey2, 0, ede, subkey1.length, subkey2.length);
            System.arraycopy(subkey1, 0, ede, subkey1.length + subkey2.length, subkey1.length);
            spec = new DESedeKeySpec(ede, 0);
            if (logger.isLoggable(Level.FINEST)) {
                traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST46:3DES key input: ", input);
                traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST47:3DES key ede: ", ede);
                traceOutput(DP_CLASS_NAME, "makeDesKeys", "DIGEST48:3DES key material: ", ((DESedeKeySpec) spec).getKey());
                logger.log(Level.FINEST, "DIGEST49: is parity-adjusted? ", Boolean.valueOf(DESedeKeySpec.isParityAdjusted(ede, 0)));
            }
            break;
        default:
            throw new IllegalArgumentException("Invalid DES strength:" + desStrength);
    }
    return desFactory.generateSecret(spec);
}
Also used : 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) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 7 with DESedeKeySpec

use of javax.crypto.spec.DESedeKeySpec in project jdk8u_jdk by JetBrains.

the class Des3DkCrypto method getCipher.

protected Cipher getCipher(byte[] key, byte[] ivec, int mode) throws GeneralSecurityException {
    // NoSuchAlgorithException
    SecretKeyFactory factory = SecretKeyFactory.getInstance("desede");
    // InvalidKeyException
    KeySpec spec = new DESedeKeySpec(key, 0);
    // InvalidKeySpecException
    SecretKey secretKey = factory.generateSecret(spec);
    // IV
    if (ivec == null) {
        ivec = ZERO_IV;
    }
    // NoSuchAlgorithmException, NoSuchPaddingException
    // NoSuchProviderException
    Cipher cipher = Cipher.getInstance("DESede/CBC/NoPadding");
    IvParameterSpec encIv = new IvParameterSpec(ivec, 0, ivec.length);
    // InvalidKeyException, InvalidAlgorithParameterException
    cipher.init(mode, secretKey, encIv);
    return cipher;
}
Also used : SecretKey(javax.crypto.SecretKey) KeySpec(java.security.spec.KeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 8 with DESedeKeySpec

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

the class CipherTest method test_update$BII.

/**
     * javax.crypto.Cipher#update(byte[], int, int)
     */
public void test_update$BII() throws Exception {
    for (int index = 1; index < 4; index++) {
        Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding");
        byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".key");
        DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE");
        Key k = skf.generateSecret(keySpec);
        byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".iv");
        IvParameterSpec iv = new IvParameterSpec(ivMaterial);
        c.init(Cipher.DECRYPT_MODE, k, iv);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] input = new byte[256];
        String resPath = "hyts_" + "des-ede3-cbc.test" + index + ".ciphertext";
        File resources = Support_Resources.createTempFolder();
        Support_Resources.copyFile(resources, null, resPath);
        InputStream is = Support_Resources.getStream(resPath);
        int bytesRead = is.read(input, 0, 256);
        while (bytesRead > 0) {
            byte[] output = c.update(input, 0, bytesRead);
            if (output != null) {
                baos.write(output);
            }
            bytesRead = is.read(input, 0, 256);
        }
        byte[] output = c.doFinal();
        if (output != null) {
            baos.write(output);
        }
        byte[] decipheredCipherText = baos.toByteArray();
        is.close();
        byte[] plaintextBytes = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".plaintext");
        assertEquals("Operation produced incorrect results for index " + index, Arrays.toString(plaintextBytes), Arrays.toString(decipheredCipherText));
    }
    Cipher cipher = Cipher.getInstance("DESEDE/CBC/PKCS5Padding");
    try {
        cipher.update(new byte[64], 0, 32);
        fail();
    } catch (IllegalStateException expected) {
    }
}
Also used : InputStream(java.io.InputStream) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SecretKeyFactory(javax.crypto.SecretKeyFactory) File(java.io.File) Key(java.security.Key)

Example 9 with DESedeKeySpec

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

the class CipherTest method test_doFinal.

/**
     * javax.crypto.Cipher#doFinal()
     */
public void test_doFinal() throws Exception {
    for (int index = 1; index < 4; index++) {
        Cipher c = Cipher.getInstance("DESEDE/CBC/PKCS5Padding");
        byte[] keyMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".key");
        DESedeKeySpec keySpec = new DESedeKeySpec(keyMaterial);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DESEDE");
        Key k = skf.generateSecret(keySpec);
        byte[] ivMaterial = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".iv");
        IvParameterSpec iv = new IvParameterSpec(ivMaterial);
        c.init(Cipher.ENCRYPT_MODE, k, iv);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] input = new byte[256];
        String resPath = "hyts_" + "des-ede3-cbc.test" + index + ".plaintext";
        File resources = Support_Resources.createTempFolder();
        Support_Resources.copyFile(resources, null, resPath);
        InputStream is = Support_Resources.getStream(resPath);
        int bytesRead = is.read(input, 0, 256);
        while (bytesRead > 0) {
            byte[] output = c.update(input, 0, bytesRead);
            if (output != null) {
                baos.write(output);
            }
            bytesRead = is.read(input, 0, 256);
        }
        byte[] output = c.doFinal();
        if (output != null) {
            baos.write(output);
        }
        byte[] encryptedPlaintext = baos.toByteArray();
        is.close();
        byte[] cipherText = loadBytes("hyts_" + "des-ede3-cbc.test" + index + ".ciphertext");
        assertEquals("Operation produced incorrect results for index " + index, Arrays.toString(cipherText), Arrays.toString(encryptedPlaintext));
    }
    byte[] b = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
    byte[] b1 = new byte[30];
    AlgorithmParameterSpec ap = new IvParameterSpec(IV);
    Cipher c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    c.update(b, 0, 10, b1, 5);
    try {
        c.doFinal();
        fail();
    } catch (IllegalBlockSizeException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    try {
        c.doFinal();
        fail();
    } catch (IllegalStateException expected) {
    }
    c = Cipher.getInstance("DES/CBC/NoPadding");
    c.init(Cipher.ENCRYPT_MODE, CIPHER_KEY_DES, ap);
    int len = c.doFinal(b, 0, 16, b1, 0);
    assertEquals(16, len);
    c = Cipher.getInstance("DES/CBC/PKCS5Padding");
    c.init(Cipher.DECRYPT_MODE, CIPHER_KEY_DES, ap);
    assertTrue(Arrays.equals(c.getIV(), IV));
    c.update(b1, 0, 24, b, 0);
    try {
        c.doFinal();
        fail();
    } catch (BadPaddingException expected) {
    }
}
Also used : InputStream(java.io.InputStream) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BadPaddingException(javax.crypto.BadPaddingException) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) MyCipher(org.apache.harmony.crypto.tests.support.MyCipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) File(java.io.File) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) Key(java.security.Key)

Example 10 with DESedeKeySpec

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

the class StringEncrypter method encrypt.

/**
	 * Encrypt a string
	 * 
	 * @param unencryptedString
	 *            String to encrypt
	 * @return Encrypted string (using scheme and key specified at construction)
	 * @throws EncryptionException
	 */
public String encrypt(final String unencryptedString, 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 encrypt(unencryptedString, 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