Search in sources :

Example 41 with KeySpec

use of java.security.spec.KeySpec in project protools by SeanDragon.

the class ToolPbkdf2 method getEncryptedPassword.

public static byte[] getEncryptedPassword(String password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException {
    // PBKDF2 with SHA-1 as the hashing algorithm. Note that the NIST
    // specifically names SHA-1 as an acceptable hashing algorithm for
    // PBKDF2
    String algorithm = "PBKDF2WithHmacSHA1";
    // SHA-1 generates 160 bit hashes, so that's what makes sense here
    int derivedKeyLength = 160;
    // Pick an iteration count that works for you. The NIST recommends at
    // least 1,000 iterations:
    // http://csrc.nist.gov/publications/nistpubs/800-132/nist-sp800-132.pdf
    // iOS 4.x reportedly uses 10,000:
    // http://blog.crackpassword.com/2010/09/smartphone-forensics-cracking-blackberry-backup-passwords/
    int iterations = 20000;
    KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, iterations, derivedKeyLength);
    SecretKeyFactory f = SecretKeyFactory.getInstance(algorithm);
    return f.generateSecret(spec).getEncoded();
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 42 with KeySpec

use of java.security.spec.KeySpec in project jackrabbit-oak by apache.

the class PasswordUtil method generatePBKDF2.

@Nonnull
private static String generatePBKDF2(@Nonnull String pwd, @Nonnull String salt, @Nonnull String algorithm, int iterations, int keyLength) throws NoSuchAlgorithmException {
    // for example PBKDF2WithHmacSHA1
    SecretKeyFactory factory = SecretKeyFactory.getInstance(algorithm);
    byte[] saltBytes = convertHexToBytes(salt);
    KeySpec keyspec = new PBEKeySpec(pwd.toCharArray(), saltBytes, iterations, keyLength);
    try {
        Key key = factory.generateSecret(keyspec);
        byte[] bytes = key.getEncoded();
        return convertBytesToHex(bytes);
    } catch (InvalidKeySpecException e) {
        throw new NoSuchAlgorithmException(algorithm, e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SecretKeyFactory(javax.crypto.SecretKeyFactory) Key(java.security.Key) Nonnull(javax.annotation.Nonnull)

Example 43 with KeySpec

use of java.security.spec.KeySpec in project BlogSource by TeachCourse.

the class BrokenKeyDerivationActivity method deriveKeySecurely.

/**
 * Example use of a key derivation function, derivating a key securely from a password.
 */
private SecretKey deriveKeySecurely(String password, int keySizeInBytes) {
    // Use this to derive the key from the password:
    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), retrieveSalt(), 100, /* iterationCount */
    keySizeInBytes * 8);
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        byte[] keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
        return new SecretKeySpec(keyBytes, "AES");
    } catch (Exception e) {
        throw new RuntimeException("Deal with exceptions properly!", e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) IOException(java.io.IOException) GeneralSecurityException(java.security.GeneralSecurityException)

Example 44 with KeySpec

use of java.security.spec.KeySpec in project BlogSource by TeachCourse.

the class SharedPreferencesActivity method obtainSecretKey.

/**
 * 手动生成一个SecretKey
 */
private SecretKey obtainSecretKey(String password, int keySize) {
    /* Store these things on disk used to derive key later: */
    int iterationCount = 1000;
    // bytes; should be the same sizeas the output (256 / 8 = 32)
    int saltLength = 32;
    // 256-bits for AES-256, 128-bits for AES-128, etc
    int keyLength = keySize * 8;
    // Should be of saltLength
    byte[] salt;
    /* When first creating the key, obtain a salt with this: */
    SecureRandom random = new SecureRandom();
    salt = new byte[saltLength];
    random.nextBytes(salt);
    /* Use this to derive the key from the password: */
    KeySpec keySpec = new PBEKeySpec(password.toCharArray(), salt, iterationCount, keyLength);
    byte[] keyBytes = new byte[0];
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        keyBytes = keyFactory.generateSecret(keySpec).getEncoded();
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (InvalidKeySpecException e) {
        e.printStackTrace();
    }
    SecretKey key = new SecretKeySpec(keyBytes, "AES");
    return key;
}
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) SecureRandom(java.security.SecureRandom) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 45 with KeySpec

use of java.security.spec.KeySpec in project incubator-pulsar by apache.

the class SecurityUtility method loadPrivateKeyFromPemFile.

public static PrivateKey loadPrivateKeyFromPemFile(String keyFilePath) throws KeyManagementException {
    PrivateKey privateKey = null;
    if (keyFilePath == null || keyFilePath.isEmpty()) {
        return privateKey;
    }
    try (BufferedReader reader = new BufferedReader(new FileReader(keyFilePath))) {
        StringBuilder sb = new StringBuilder();
        String previousLine = "";
        String currentLine = null;
        // Skip the first line (-----BEGIN RSA PRIVATE KEY-----)
        reader.readLine();
        while ((currentLine = reader.readLine()) != null) {
            sb.append(previousLine);
            previousLine = currentLine;
        }
        // Skip the last line (-----END RSA PRIVATE KEY-----)
        KeyFactory kf = KeyFactory.getInstance("RSA");
        KeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(sb.toString()));
        privateKey = kf.generatePrivate(keySpec);
    } catch (GeneralSecurityException | IOException e) {
        throw new KeyManagementException("Private key loading error", e);
    }
    return privateKey;
}
Also used : PrivateKey(java.security.PrivateKey) KeySpec(java.security.spec.KeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) IOException(java.io.IOException) KeyFactory(java.security.KeyFactory) KeyManagementException(java.security.KeyManagementException)

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