Search in sources :

Example 31 with KeySpec

use of java.security.spec.KeySpec 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 32 with KeySpec

use of java.security.spec.KeySpec 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 33 with KeySpec

use of java.security.spec.KeySpec in project android_frameworks_base by DirtyUnicorns.

the class BackupManagerService method buildCharArrayKey.

private SecretKey buildCharArrayKey(String algorithm, char[] pwArray, byte[] salt, int rounds) {
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
        KeySpec ks = new PBEKeySpec(pwArray, salt, rounds, PBKDF2_KEY_SIZE);
        return keyFactory.generateSecret(ks);
    } catch (InvalidKeySpecException e) {
        Slog.e(TAG, "Invalid key spec for PBKDF2!");
    } catch (NoSuchAlgorithmException e) {
        Slog.e(TAG, "PBKDF2 unavailable!");
    }
    return null;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 34 with KeySpec

use of java.security.spec.KeySpec 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)

Example 35 with KeySpec

use of java.security.spec.KeySpec in project oxCore by GluuFederation.

the class SamlConfiguration method loadPrivateKey.

/**
 * loads the private key for digital signature
 *
 * @param prvKeyPath
 *            file path to location of private key
 * @throws Exception
 */
public void loadPrivateKey(String prvKeyPath) throws Exception {
    OpenSAMLUtil.initSamlEngine();
    File privKeyFile = new File(prvKeyPath);
    BufferedInputStream bis = null;
    try {
        bis = new BufferedInputStream(new FileInputStream(privKeyFile));
    } catch (FileNotFoundException e) {
        throw new Exception("Could not locate keyfile at '" + prvKeyPath + "'", e);
    }
    byte[] privKeyBytes = new byte[(int) privKeyFile.length()];
    bis.read(privKeyBytes);
    bis.close();
    KeySpec ks = new PKCS8EncodedKeySpec(privKeyBytes);
    KeyFactory keyFactory = KeyFactory.getInstance("RSA");
    privateKey = keyFactory.generatePrivate(ks);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) FileInputStream(java.io.FileInputStream) CertificateException(java.security.cert.CertificateException) CloneFailedException(org.gluu.saml.exception.CloneFailedException) FileNotFoundException(java.io.FileNotFoundException) KeyFactory(java.security.KeyFactory)

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