Search in sources :

Example 36 with KeySpec

use of java.security.spec.KeySpec in project audit4j-core by audit4j.

the class EncryptionUtil method generateKeyIfNotAvailable.

/**
 * Generate key.
 *
 * @param key
 *            the key
 * @param salt
 *            the salt
 * @return the key
 * @throws NoSuchAlgorithmException
 *             the no such algorithm exception
 * @throws UnsupportedEncodingException
 *             the unsupported encoding exception
 * @throws InvalidKeySpecException
 *             the invalid key spec exception
 */
private static void generateKeyIfNotAvailable(String key, String salt) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeySpecException {
    if (null == cryptKey) {
        LOG.debug("Generating Key...");
        SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        char[] password = key.toCharArray();
        byte[] saltBytes = salt.getBytes(CoreConstants.ENCODE_UTF8);
        KeySpec spec = new PBEKeySpec(password, saltBytes, 65536, 128);
        SecretKey tmp = factory.generateSecret(spec);
        byte[] encoded = tmp.getEncoded();
        cryptKey = new SecretKeySpec(encoded, "AES");
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 37 with KeySpec

use of java.security.spec.KeySpec in project smoke by textbrowser.

the class Cryptography method generateMacKey.

public static SecretKey generateMacKey(byte[] salt, char[] password, int iterations) throws InvalidKeySpecException, NoSuchAlgorithmException {
    // Bits.
    final int length = 512;
    KeySpec keySpec = new PBEKeySpec(password, salt, iterations, length);
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    return secretKeyFactory.generateSecret(keySpec);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) EncodedKeySpec(java.security.spec.EncodedKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 38 with KeySpec

use of java.security.spec.KeySpec in project smoke by textbrowser.

the class Cryptography method pbkdf2.

public static byte[] pbkdf2(byte[] salt, char[] password, int iterations, int length) {
    try {
        KeySpec keySpec = new PBEKeySpec(password, salt, iterations, length);
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        return secretKeyFactory.generateSecret(keySpec).getEncoded();
    } catch (Exception exception) {
        return null;
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) EncodedKeySpec(java.security.spec.EncodedKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidAlgorithmParameterException(java.security.InvalidAlgorithmParameterException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Example 39 with KeySpec

use of java.security.spec.KeySpec in project fdroidclient by f-droid.

the class ZipSigner method readPrivateKey.

/**
 * Read a PKCS 8 format private key.
 */
public PrivateKey readPrivateKey(URL privateKeyUrl, String keyPassword) throws IOException, GeneralSecurityException {
    DataInputStream input = new DataInputStream(privateKeyUrl.openStream());
    try {
        byte[] bytes = readContentAsBytes(input);
        KeySpec spec = decryptPrivateKey(bytes, keyPassword);
        if (spec == null) {
            spec = new PKCS8EncodedKeySpec(bytes);
        }
        try {
            return KeyFactory.getInstance("RSA").generatePrivate(spec);
        } catch (InvalidKeySpecException ex) {
            return KeyFactory.getInstance("DSA").generatePrivate(spec);
        }
    } finally {
        input.close();
    }
}
Also used : PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException)

Example 40 with KeySpec

use of java.security.spec.KeySpec in project wicket by apache.

the class SunJceCrypt method generateSecretKey.

/**
 * Generate the de-/encryption key.
 * <p>
 * Note: if you don't provide your own encryption key, the implementation will use a default. Be
 * aware that this is potential security risk. Thus make sure you always provide your own one.
 *
 * @return secretKey the security key generated
 * @throws NoSuchAlgorithmException
 *             unable to find encryption algorithm specified
 * @throws InvalidKeySpecException
 *             invalid encryption key
 */
protected SecretKey generateSecretKey() throws NoSuchAlgorithmException, InvalidKeySpecException {
    SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(cryptMethod);
    KeySpec spec = createKeySpec();
    return keyFactory.generateSecret(spec);
}
Also used : KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

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