Search in sources :

Example 21 with SecretKeyFactory

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

the class CipherTest method getEncryptKey.

private static synchronized Key getEncryptKey(String algorithm) throws Exception {
    Key key = ENCRYPT_KEYS.get(algorithm);
    if (key != null) {
        return key;
    }
    if (algorithm.startsWith("RSA")) {
        KeyFactory kf = KeyFactory.getInstance("RSA");
        RSAPrivateKeySpec keySpec = new RSAPrivateKeySpec(RSA_2048_modulus, RSA_2048_privateExponent);
        key = kf.generatePrivate(keySpec);
    } else if (isPBE(algorithm)) {
        SecretKeyFactory skf = SecretKeyFactory.getInstance(algorithm);
        key = skf.generateSecret(new PBEKeySpec("secret".toCharArray()));
    } else {
        KeyGenerator kg = KeyGenerator.getInstance(getBaseAlgorithm(algorithm));
        key = kg.generateKey();
    }
    ENCRYPT_KEYS.put(algorithm, key);
    return key;
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyGenerator(javax.crypto.KeyGenerator) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSAPrivateKey(java.security.interfaces.RSAPrivateKey) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey) SecretKey(javax.crypto.SecretKey) SecretKeyFactory(javax.crypto.SecretKeyFactory) KeyFactory(java.security.KeyFactory)

Example 22 with SecretKeyFactory

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

the class SecretKeyFactoryTest method test_PBKDF2_UTF8.

private void test_PBKDF2_UTF8(char[] password, byte[] salt, int iterations, int keyLength, byte[] expected) throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    KeySpec ks = new PBEKeySpec(password, salt, iterations, keyLength);
    SecretKey key = factory.generateSecret(ks);
    assertTrue(Arrays.equals(expected, key.getEncoded()));
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 23 with SecretKeyFactory

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

the class SecretKeyFactoryTest method test_PBKDF2_required_parameters.

public void test_PBKDF2_required_parameters() throws Exception {
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
    // PBEKeySpecs password only constructor
    try {
        KeySpec ks = new PBEKeySpec(null);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(new char[0]);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(PASSWORD);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    // PBEKeySpecs constructor without key length
    try {
        KeySpec ks = new PBEKeySpec(null, SALT, ITERATIONS);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(new char[0], SALT, ITERATIONS);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(PASSWORD, SALT, ITERATIONS);
        factory.generateSecret(ks);
        fail();
    } catch (InvalidKeySpecException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(null, SALT, ITERATIONS, KEY_LENGTH);
        factory.generateSecret(ks);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        KeySpec ks = new PBEKeySpec(new char[0], SALT, ITERATIONS, KEY_LENGTH);
        factory.generateSecret(ks);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    KeySpec ks = new PBEKeySpec(PASSWORD, SALT, ITERATIONS, KEY_LENGTH);
    factory.generateSecret(ks);
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 24 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project ButterRemote-Android by se-bastiaan.

the class ObscuredSharedPreferences method decrypt.

protected String decrypt(String value) {
    try {
        final byte[] bytes = value != null ? Base64.decode(value, Base64.DEFAULT) : new byte[0];
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(PBE_WITH_MD5_AND_DES);
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(SEKRIT));
        Cipher pbeCipher = Cipher.getInstance(PBE_WITH_MD5_AND_DES);
        pbeCipher.init(Cipher.DECRYPT_MODE, key, new PBEParameterSpec(Settings.Secure.getString(context.getContentResolver(), Settings.System.ANDROID_ID).getBytes(UTF8), 20));
        return new String(pbeCipher.doFinal(bytes), UTF8);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 25 with SecretKeyFactory

use of javax.crypto.SecretKeyFactory in project camel by apache.

the class XMLSecurityDataFormat method generateKeyEncryptionKey.

private Key generateKeyEncryptionKey(String algorithm) throws InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
    DESedeKeySpec keySpec;
    Key secretKey;
    try {
        if (algorithm.equalsIgnoreCase("DESede")) {
            keySpec = new DESedeKeySpec(passPhrase);
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(algorithm);
            secretKey = keyFactory.generateSecret(keySpec);
        } else if (algorithm.equalsIgnoreCase("SEED")) {
            secretKey = new SecretKeySpec(passPhrase, "SEED");
        } else if (algorithm.equalsIgnoreCase("CAMELLIA")) {
            secretKey = new SecretKeySpec(passPhrase, "CAMELLIA");
        } else {
            secretKey = new SecretKeySpec(passPhrase, "AES");
        }
        if (Arrays.equals(passPhrase, DEFAULT_KEY.getBytes())) {
            LOG.warn("Using the default encryption key is not secure");
        }
    } catch (InvalidKeyException e) {
        throw new InvalidKeyException("InvalidKeyException due to invalid passPhrase: " + Arrays.toString(passPhrase));
    } catch (NoSuchAlgorithmException e) {
        throw new NoSuchAlgorithmException("NoSuchAlgorithmException while using algorithm: " + algorithm);
    } catch (InvalidKeySpecException e) {
        throw new InvalidKeySpecException("Invalid Key generated while using passPhrase: " + Arrays.toString(passPhrase));
    }
    return secretKey;
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) DESedeKeySpec(javax.crypto.spec.DESedeKeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException) SecretKeyFactory(javax.crypto.SecretKeyFactory) PublicKey(java.security.PublicKey) EncryptedKey(org.apache.xml.security.encryption.EncryptedKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey)

Aggregations

SecretKeyFactory (javax.crypto.SecretKeyFactory)129 SecretKey (javax.crypto.SecretKey)84 PBEKeySpec (javax.crypto.spec.PBEKeySpec)75 Cipher (javax.crypto.Cipher)58 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)39 DESKeySpec (javax.crypto.spec.DESKeySpec)28 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)26 PBEParameterSpec (javax.crypto.spec.PBEParameterSpec)26 KeySpec (java.security.spec.KeySpec)25 SecretKeySpec (javax.crypto.spec.SecretKeySpec)23 SecureRandom (java.security.SecureRandom)18 KeyStoreException (java.security.KeyStoreException)16 IOException (java.io.IOException)15 InvalidKeyException (java.security.InvalidKeyException)14 PrivateKey (java.security.PrivateKey)12 CertificateException (java.security.cert.CertificateException)12 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)12 UnrecoverableKeyException (java.security.UnrecoverableKeyException)11 Key (java.security.Key)10 KeyFactory (java.security.KeyFactory)10