Search in sources :

Example 46 with KeySpec

use of java.security.spec.KeySpec in project ORCID-Source by ORCID.

the class DesEncrypter method initDesEncrypter.

private void initDesEncrypter(final String passPhrase) {
    try {
        // Create the key
        KeySpec keySpec = new PBEKeySpec(passPhrase.toCharArray(), salt, iterationCount);
        SecretKey key = SecretKeyFactory.getInstance("PBEWithMD5AndDES").generateSecret(keySpec);
        ecipher = Cipher.getInstance(key.getAlgorithm());
        dcipher = Cipher.getInstance(key.getAlgorithm());
        // Prepare the parameter to the ciphers
        AlgorithmParameterSpec paramSpec = new PBEParameterSpec(salt, iterationCount);
        // Create the ciphers
        ecipher.init(Cipher.ENCRYPT_MODE, key, paramSpec);
        dcipher.init(Cipher.DECRYPT_MODE, key, paramSpec);
    } catch (GeneralSecurityException e) {
        LOGGER.trace("DesEncrypter.creation failed", e);
        throw new ApplicationException("DesEncrypter creation failed", e);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) ApplicationException(org.orcid.core.exception.ApplicationException) KeySpec(java.security.spec.KeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) GeneralSecurityException(java.security.GeneralSecurityException) AlgorithmParameterSpec(java.security.spec.AlgorithmParameterSpec) PBEParameterSpec(javax.crypto.spec.PBEParameterSpec)

Example 47 with KeySpec

use of java.security.spec.KeySpec in project j2objc by google.

the class KeyFactorySpiTest method testKeyFactorySpi.

/**
     * Test for <code>KeyFactorySpi</code> constructor
     * Assertion: constructs KeyFactorySpi
     */
public void testKeyFactorySpi() {
    MyKeyFactorySpi keyFSpi = new MyKeyFactorySpi();
    assertTrue(keyFSpi instanceof KeyFactorySpi);
    KeySpec ks = new MyKeySpec();
    KeySpec kss = new MyKeySpec();
    try {
        keyFSpi.engineGeneratePrivate(ks);
        keyFSpi.engineGeneratePublic(ks);
        keyFSpi.engineGetKeySpec(null, java.lang.Class.class);
        keyFSpi.engineTranslateKey(null);
    } catch (Exception e) {
        fail("Unexpected exception");
    }
}
Also used : KeyFactorySpi(java.security.KeyFactorySpi) KeySpec(java.security.spec.KeySpec)

Example 48 with KeySpec

use of java.security.spec.KeySpec in project j2objc by google.

the class KeyFactoryTest method testGetKeySpec.

@SuppressWarnings("unchecked")
public void testGetKeySpec() {
    KeyFactory factory = null;
    try {
        factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
    } catch (NoSuchAlgorithmException e) {
        fail("unexpected exception: " + e);
    }
    assertNotNull(factory);
    {
        Key[] keys = { new TestPrivateKey(), new TestPublicKey(), new TestPrivateKey(new byte[] { 42, 41, 40 }), new TestPublicKey(new byte[] { 40, 41, 42 }) };
        Class[] keySpecs = { TestPrivateKeySpec.class, TestPublicKeySpec.class, TestPrivateKeySpec.class, TestPublicKeySpec.class };
        for (int i = 0; i < keys.length; i++) {
            Key key = keys[i];
            Class keySpec = keySpecs[i];
            String message = "getKeySpec(" + key.toString() + ", " + keySpec.toString() + ")";
            try {
                KeySpec spec = factory.getKeySpec(key, keySpec);
                assertNotNull(spec);
                assertTrue(spec.getClass() == keySpec);
            } catch (InvalidKeySpecException e) {
                fail("unexpected exception: " + e);
            }
        }
    }
    {
        Key[] keys = { new AnotherKey(), null, new TestPrivateKey(), null };
        Class[] keySpecs = { KeySpec.class, TestPrivateKeySpec.class, null, null };
        Class[] exceptions = { InvalidKeySpecException.class, NullPointerException.class, InvalidKeySpecException.class, NullPointerException.class };
        for (int i = 0; i < keys.length; i++) {
            Key key = keys[i];
            Class keySpec = keySpecs[i];
            exceptionThrown = false;
            String message = "getKeySpec(" + (key == null ? "null" : key.toString()) + ", " + (keySpec == null ? "null" : keySpec.toString()) + ")";
            try {
                factory.getKeySpec(key, keySpec);
            } catch (Exception e) {
                checkException(message, e, exceptions[i]);
            } finally {
                checkException(message, null, exceptions[i]);
            }
        }
    }
}
Also used : KeySpec(java.security.spec.KeySpec) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvocationTargetException(java.lang.reflect.InvocationTargetException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeyException(java.security.InvalidKeyException) NoSuchProviderException(java.security.NoSuchProviderException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) KeyFactory(java.security.KeyFactory) PublicKey(java.security.PublicKey) Key(java.security.Key) PrivateKey(java.security.PrivateKey)

Example 49 with KeySpec

use of java.security.spec.KeySpec in project j2objc by google.

the class RSAPublicKeySpecTest method testRSAPublicKeySpec02.

/**
     * Test #2 for <code>RSAPublicKeySpec</code> constructor
     * Assertion: Constructs <code>RSAPublicKeySpec</code>
     * object using valid parameters
     */
public final void testRSAPublicKeySpec02() {
    KeySpec ks = new RSAPublicKeySpec(null, null);
    assertTrue(ks instanceof RSAPublicKeySpec);
}
Also used : KeySpec(java.security.spec.KeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec) RSAPublicKeySpec(java.security.spec.RSAPublicKeySpec)

Example 50 with KeySpec

use of java.security.spec.KeySpec in project robovm by robovm.

the class SubjectPublicKeyInfo method getPublicKey.

/**
     * Returns the PublicKey corresponding to this SubjectPublicKeyInfo
     * instance.
     */
public PublicKey getPublicKey() {
    if (publicKey == null) {
        final byte[] encoded = getEncoded();
        final KeySpec keySpec = new X509EncodedKeySpec(encoded);
        /* Try using the algorithm name first. */
        final String algName = algorithmID.getAlgorithmName();
        publicKey = generateKeyForAlgorithm(keySpec, algName);
        /*
             * Fall back to using the algorithm OID if it's not the same as the
             * algorithm name.
             */
        final String algOid = algorithmID.getAlgorithm();
        if (publicKey == null && !algOid.equals(algName)) {
            publicKey = generateKeyForAlgorithm(keySpec, algOid);
        }
        /*
             * Encode this as an X.509 public key since we didn't have any
             * KeyFactory that could handle this algorithm name or OID. Perhaps
             * the thing that's using this can decode it.
             */
        if (publicKey == null) {
            publicKey = new X509PublicKey(algOid, encoded, subjectPublicKey);
        }
    }
    return publicKey;
}
Also used : KeySpec(java.security.spec.KeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) ASN1BitString(org.apache.harmony.security.asn1.ASN1BitString) BitString(org.apache.harmony.security.asn1.BitString)

Aggregations

KeySpec (java.security.spec.KeySpec)79 PBEKeySpec (javax.crypto.spec.PBEKeySpec)29 SecretKeyFactory (javax.crypto.SecretKeyFactory)27 KeyFactory (java.security.KeyFactory)21 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)21 SecretKeySpec (javax.crypto.spec.SecretKeySpec)21 InvalidKeySpecException (java.security.spec.InvalidKeySpecException)20 SecretKey (javax.crypto.SecretKey)19 PKCS8EncodedKeySpec (java.security.spec.PKCS8EncodedKeySpec)15 RSAPrivateKeySpec (java.security.spec.RSAPrivateKeySpec)12 PrivateKey (java.security.PrivateKey)11 X509EncodedKeySpec (java.security.spec.X509EncodedKeySpec)11 InvalidKeyException (java.security.InvalidKeyException)9 RSAPublicKeySpec (java.security.spec.RSAPublicKeySpec)9 DESKeySpec (javax.crypto.spec.DESKeySpec)9 IOException (java.io.IOException)8 BigInteger (java.math.BigInteger)7 NoSuchProviderException (java.security.NoSuchProviderException)7 PublicKey (java.security.PublicKey)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6