Search in sources :

Example 6 with KeySpec

use of java.security.spec.KeySpec in project jdk8u_jdk by JetBrains.

the class GenerationTests method getPrivateKey.

private static PrivateKey getPrivateKey(String algo, int keysize) throws Exception {
    KeyFactory kf = KeyFactory.getInstance(algo);
    KeySpec kspec;
    if (algo.equalsIgnoreCase("DSA")) {
        if (keysize == 1024) {
            kspec = new DSAPrivateKeySpec(new BigInteger(DSA_X), new BigInteger(DSA_P), new BigInteger(DSA_Q), new BigInteger(DSA_G));
        } else if (keysize == 2048) {
            kspec = new DSAPrivateKeySpec(new BigInteger(DSA_2048_X), new BigInteger(DSA_2048_P), new BigInteger(DSA_2048_Q), new BigInteger(DSA_2048_G));
        } else
            throw new RuntimeException("Unsupported keysize:" + keysize);
    } else if (algo.equalsIgnoreCase("RSA")) {
        if (keysize == 512) {
            kspec = new RSAPrivateKeySpec(new BigInteger(RSA_MOD), new BigInteger(RSA_PRIV));
        } else {
            kspec = new RSAPrivateKeySpec(new BigInteger(RSA_1024_MOD), new BigInteger(RSA_1024_PRIV));
        }
    } else
        throw new RuntimeException("Unsupported key algorithm " + algo);
    return kf.generatePrivate(kspec);
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) RSAPrivateKeySpec(java.security.spec.RSAPrivateKeySpec) BigInteger(java.math.BigInteger) KeyFactory(java.security.KeyFactory)

Example 7 with KeySpec

use of java.security.spec.KeySpec in project jdk8u_jdk by JetBrains.

the class TestKeyStoreBasic method runTest.

public void runTest(String provider) throws Exception {
    // load private key
    // all keystore types should support private keys
    KeySpec spec = new PKCS8EncodedKeySpec(Base64.getMimeDecoder().decode(PRIVATE_KEY_PKCS8_BASE64));
    PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(spec);
    // load x509 certificate
    Certificate cert;
    try (InputStream is = new BufferedInputStream(new ByteArrayInputStream(CERTIFICATE.getBytes()))) {
        cert = CertificateFactory.getInstance("X.509").generateCertificate(is);
    }
    int numEntries = 5;
    String type = null;
    for (int i = 0; i < PROVIDERS.length; i++) {
        if (provider.compareTo(PROVIDERS[i]) == 0) {
            type = KS_Type[i];
            break;
        }
    }
    System.out.printf("Test %s provider and %s keystore%n", provider, type);
    KeyStore ks = KeyStore.getInstance(type, provider);
    KeyStore ks2 = KeyStore.getInstance(type, ks.getProvider().getName());
    // create an empty key store
    ks.load(null, null);
    // store the secret keys
    for (int j = 0; j < numEntries; j++) {
        ks.setKeyEntry(ALIAS_HEAD + j, privateKey, PASSWDK, new Certificate[] { cert });
    }
    // initialize the 2nd key store object with the 1st one
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ks.store(baos, PASSWDK);
    byte[] bArr = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(bArr);
    ks2.load(bais, null);
    // check 2nd key store type
    checkType(ks2, type);
    // check the existing aliases for the 2nd key store
    checkAlias(ks2, numEntries);
    // compare the creation date of the 2 key stores for all aliases
    compareCreationDate(ks, ks2, numEntries);
    // remove the last entry from the 2nd key store
    numEntries--;
    ks2.deleteEntry(ALIAS_HEAD + numEntries);
    // re-initialize the 1st key store with the 2nd key store
    baos.reset();
    ks2.store(baos, PASSWD2);
    bais = new ByteArrayInputStream(baos.toByteArray());
    try {
        // expect an exception since the password is incorrect
        ks.load(bais, PASSWDK);
        throw new RuntimeException("ERROR: passed the loading with incorrect password");
    } catch (IOException ex) {
        System.out.println("Expected exception: " + ex);
        if (!causedBy(ex, UnrecoverableKeyException.class)) {
            ex.printStackTrace(System.out);
            throw new RuntimeException("Unexpected cause");
        }
        System.out.println("Expected cause: " + UnrecoverableKeyException.class.getName());
        bais.reset();
        ks.load(bais, PASSWD2);
        bais.reset();
        ks.load(bais, null);
    }
    // check key store type
    checkType(ks, type);
    // check the existing aliases
    checkAlias(ks, numEntries);
    // compare the creation date of the 2 key stores for all aliases
    compareCreationDate(ks, ks2, numEntries);
}
Also used : PrivateKey(java.security.PrivateKey) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) KeySpec(java.security.spec.KeySpec) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) KeyStore(java.security.KeyStore) UnrecoverableKeyException(java.security.UnrecoverableKeyException) BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) Certificate(java.security.cert.Certificate)

Example 8 with KeySpec

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

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 9 with KeySpec

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

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

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