Search in sources :

Example 26 with SecretKey

use of javax.crypto.SecretKey in project storm by nathanmarz.

the class BlowfishTupleSerializer method main.

/**
     * Produce a blowfish key to be used in "Storm jar" command
     */
public static void main(String[] args) {
    try {
        KeyGenerator kgen = KeyGenerator.getInstance("Blowfish");
        SecretKey skey = kgen.generateKey();
        byte[] raw = skey.getEncoded();
        String keyString = new String(Hex.encodeHex(raw));
        System.out.println("storm -c " + SECRET_KEY + "=" + keyString + " -c " + Config.TOPOLOGY_TUPLE_SERIALIZER + "=" + BlowfishTupleSerializer.class.getName() + " ...");
    } catch (Exception ex) {
        LOG.error(ex.getMessage());
        ex.printStackTrace();
    }
}
Also used : SecretKey(javax.crypto.SecretKey) KeyGenerator(javax.crypto.KeyGenerator)

Example 27 with SecretKey

use of javax.crypto.SecretKey in project netty by netty.

the class SslContext method generateKeySpec.

/**
     * Generates a key specification for an (encrypted) private key.
     *
     * @param password characters, if {@code null} an unencrypted key is assumed
     * @param key bytes of the DER encoded private key
     *
     * @return a key specification
     *
     * @throws IOException if parsing {@code key} fails
     * @throws NoSuchAlgorithmException if the algorithm used to encrypt {@code key} is unkown
     * @throws NoSuchPaddingException if the padding scheme specified in the decryption algorithm is unkown
     * @throws InvalidKeySpecException if the decryption key based on {@code password} cannot be generated
     * @throws InvalidKeyException if the decryption key based on {@code password} cannot be used to decrypt
     *                             {@code key}
     * @throws InvalidAlgorithmParameterException if decryption algorithm parameters are somehow faulty
     */
protected static PKCS8EncodedKeySpec generateKeySpec(char[] password, byte[] key) throws IOException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeySpecException, InvalidKeyException, InvalidAlgorithmParameterException {
    if (password == null) {
        return new PKCS8EncodedKeySpec(key);
    }
    EncryptedPrivateKeyInfo encryptedPrivateKeyInfo = new EncryptedPrivateKeyInfo(key);
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(encryptedPrivateKeyInfo.getAlgName());
    PBEKeySpec pbeKeySpec = new PBEKeySpec(password);
    SecretKey pbeKey = keyFactory.generateSecret(pbeKeySpec);
    Cipher cipher = Cipher.getInstance(encryptedPrivateKeyInfo.getAlgName());
    cipher.init(Cipher.DECRYPT_MODE, pbeKey, encryptedPrivateKeyInfo.getAlgParameters());
    return encryptedPrivateKeyInfo.getKeySpec(cipher);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) EncryptedPrivateKeyInfo(javax.crypto.EncryptedPrivateKeyInfo) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 28 with SecretKey

use of javax.crypto.SecretKey in project orientdb by orientechnologies.

the class OSymmetricKey method fromKeystore.

/**
   * Creates an OSymmetricKey from a Java "JCEKS" KeyStore.
   * @param is The InputStream used to load the KeyStore.
   * @param password The password for the KeyStore.  May be null.
   * @param keyAlias The alias name of the key to be used from the KeyStore.  Required.
   * @param keyPassword The password of the key represented by keyAlias.  May be null.
   */
public static OSymmetricKey fromKeystore(final InputStream is, final String password, final String keyAlias, final String keyPassword) {
    OSymmetricKey sk = null;
    try {
        // JCEKS is required to hold SecretKey entries.
        KeyStore ks = KeyStore.getInstance("JCEKS");
        char[] ksPasswdChars = null;
        if (password != null)
            ksPasswdChars = password.toCharArray();
        // ksPasswdChars may be null.
        ks.load(is, ksPasswdChars);
        char[] ksKeyPasswdChars = null;
        if (keyPassword != null)
            ksKeyPasswdChars = keyPassword.toCharArray();
        // ksKeyPasswdChars may be null.
        KeyStore.ProtectionParameter protParam = new KeyStore.PasswordProtection(ksKeyPasswdChars);
        KeyStore.SecretKeyEntry skEntry = (KeyStore.SecretKeyEntry) ks.getEntry(keyAlias, protParam);
        if (skEntry == null)
            throw new OSecurityException("SecretKeyEntry is null for key alias: " + keyAlias);
        SecretKey secretKey = skEntry.getSecretKey();
        sk = new OSymmetricKey(secretKey);
    } catch (Exception ex) {
        throw new OSecurityException("OSymmetricKey.fromKeystore() Exception: " + ex.getMessage());
    }
    return sk;
}
Also used : SecretKey(javax.crypto.SecretKey) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) KeyStore(java.security.KeyStore) OException(com.orientechnologies.common.exception.OException) KeyStoreException(java.security.KeyStoreException) NoSuchPaddingException(javax.crypto.NoSuchPaddingException) OSecurityException(com.orientechnologies.orient.core.exception.OSecurityException) IllegalBlockSizeException(javax.crypto.IllegalBlockSizeException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BadPaddingException(javax.crypto.BadPaddingException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 29 with SecretKey

use of javax.crypto.SecretKey in project platformlayer by platformlayer.

the class NexusLdapPasswords method buildCipher.

private Cipher buildCipher(String passphrase, byte[] salt, int mode) throws InvalidKeySpecException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException {
    KeySpec keySpec = new PBEKeySpec(passphrase.toCharArray());
    SecretKey key = SecretKeyFactory.getInstance(KEY_ALGORITHM, bouncyCastleProvider).generateSecret(keySpec);
    Cipher cipher = Cipher.getInstance(KEY_ALGORITHM, bouncyCastleProvider);
    PBEParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
    cipher.init(mode, key, paramSpec);
    return cipher;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) Cipher(javax.crypto.Cipher) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 30 with SecretKey

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

the class PKCS12KeyStoreSpi method unwrapKey.

protected PrivateKey unwrapKey(AlgorithmIdentifier algId, byte[] data, char[] password, boolean wrongPKCS12Zero) throws IOException {
    ASN1ObjectIdentifier algorithm = algId.getAlgorithm();
    try {
        if (algorithm.on(PKCSObjectIdentifiers.pkcs_12PbeIds)) {
            PKCS12PBEParams pbeParams = PKCS12PBEParams.getInstance(algId.getParameters());
            PBEKeySpec pbeSpec = new PBEKeySpec(password);
            PrivateKey out;
            SecretKeyFactory keyFact = SecretKeyFactory.getInstance(algorithm.getId(), bcProvider);
            PBEParameterSpec defParams = new PBEParameterSpec(pbeParams.getIV(), pbeParams.getIterations().intValue());
            SecretKey k = keyFact.generateSecret(pbeSpec);
            ((BCPBEKey) k).setTryWrongPKCS12Zero(wrongPKCS12Zero);
            Cipher cipher = Cipher.getInstance(algorithm.getId(), bcProvider);
            cipher.init(Cipher.UNWRAP_MODE, k, defParams);
            // we pass "" as the key algorithm type as it is unknown at this point
            return (PrivateKey) cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
        } else if (algorithm.equals(PKCSObjectIdentifiers.id_PBES2)) {
            PBES2Parameters alg = PBES2Parameters.getInstance(algId.getParameters());
            PBKDF2Params func = PBKDF2Params.getInstance(alg.getKeyDerivationFunc().getParameters());
            SecretKeyFactory keyFact = SecretKeyFactory.getInstance(alg.getKeyDerivationFunc().getAlgorithm().getId(), bcProvider);
            SecretKey k = keyFact.generateSecret(new PBEKeySpec(password, func.getSalt(), func.getIterationCount().intValue(), SecretKeyUtil.getKeySize(alg.getEncryptionScheme().getAlgorithm())));
            Cipher cipher = Cipher.getInstance(alg.getEncryptionScheme().getAlgorithm().getId(), bcProvider);
            cipher.init(Cipher.UNWRAP_MODE, k, new IvParameterSpec(ASN1OctetString.getInstance(alg.getEncryptionScheme().getParameters()).getOctets()));
            // we pass "" as the key algorithm type as it is unknown at this point
            return (PrivateKey) cipher.unwrap(data, "", Cipher.PRIVATE_KEY);
        }
    } catch (Exception e) {
        throw new IOException("exception unwrapping private key - " + e.toString());
    }
    throw new IOException("exception unwrapping private key - cannot recognise: " + algorithm);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) PBES2Parameters(org.bouncycastle.asn1.pkcs.PBES2Parameters) PrivateKey(java.security.PrivateKey) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SecretKey(javax.crypto.SecretKey) PKCS12PBEParams(org.bouncycastle.asn1.pkcs.PKCS12PBEParams) BCPBEKey(org.bouncycastle.jcajce.provider.symmetric.util.BCPBEKey) PBKDF2Params(org.bouncycastle.asn1.pkcs.PBKDF2Params) IvParameterSpec(javax.crypto.spec.IvParameterSpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) ASN1ObjectIdentifier(org.bouncycastle.asn1.ASN1ObjectIdentifier) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Aggregations

SecretKey (javax.crypto.SecretKey)437 Cipher (javax.crypto.Cipher)160 SecretKeySpec (javax.crypto.spec.SecretKeySpec)127 KeyGenerator (javax.crypto.KeyGenerator)112 SecretKeyFactory (javax.crypto.SecretKeyFactory)83 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)78 SecureRandom (java.security.SecureRandom)58 InvalidKeyException (java.security.InvalidKeyException)57 PBEKeySpec (javax.crypto.spec.PBEKeySpec)53 IvParameterSpec (javax.crypto.spec.IvParameterSpec)42 IOException (java.io.IOException)41 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)34 NoSuchPaddingException (javax.crypto.NoSuchPaddingException)32 KeyStore (java.security.KeyStore)30 IllegalBlockSizeException (javax.crypto.IllegalBlockSizeException)30 Test (org.junit.Test)30 BadPaddingException (javax.crypto.BadPaddingException)29 PrivateKey (java.security.PrivateKey)28 Mac (javax.crypto.Mac)28 GeneralSecurityException (java.security.GeneralSecurityException)26