Search in sources :

Example 66 with KeySpec

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

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 : DSAPublicKeySpec(java.security.spec.DSAPublicKeySpec) 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 67 with KeySpec

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

the class DSAPrivateKeySpecTest method testDSAPrivateKeySpec.

/**
     * Test for constructor
     */
public final void testDSAPrivateKeySpec() {
    KeySpec ks = new DSAPrivateKeySpec(new BigInteger("1"), new BigInteger("2"), new BigInteger("3"), new BigInteger("4"));
    assertTrue(ks instanceof DSAPrivateKeySpec);
}
Also used : DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) KeySpec(java.security.spec.KeySpec) DSAPrivateKeySpec(java.security.spec.DSAPrivateKeySpec) BigInteger(java.math.BigInteger)

Example 68 with KeySpec

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

the class BcKeyStoreSpi method decodeKey.

private Key decodeKey(DataInputStream dIn) throws IOException {
    int keyType = dIn.read();
    String format = dIn.readUTF();
    String algorithm = dIn.readUTF();
    byte[] enc = new byte[dIn.readInt()];
    KeySpec spec;
    dIn.readFully(enc);
    if (format.equals("PKCS#8") || format.equals("PKCS8")) {
        spec = new PKCS8EncodedKeySpec(enc);
    } else if (format.equals("X.509") || format.equals("X509")) {
        spec = new X509EncodedKeySpec(enc);
    } else if (format.equals("RAW")) {
        return new SecretKeySpec(enc, algorithm);
    } else {
        throw new IOException("Key format " + format + " not recognised!");
    }
    try {
        switch(keyType) {
            case KEY_PRIVATE:
                return KeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME).generatePrivate(spec);
            case KEY_PUBLIC:
                return KeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME).generatePublic(spec);
            case KEY_SECRET:
                return SecretKeyFactory.getInstance(algorithm, BouncyCastleProvider.PROVIDER_NAME).generateSecret(spec);
            default:
                throw new IOException("Key type " + keyType + " not recognised!");
        }
    } catch (Exception e) {
        throw new IOException("Exception creating key: " + e.toString());
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) KeySpec(java.security.spec.KeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) PBEKeySpec(javax.crypto.spec.PBEKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) PKCS8EncodedKeySpec(java.security.spec.PKCS8EncodedKeySpec) X509EncodedKeySpec(java.security.spec.X509EncodedKeySpec) IOException(java.io.IOException) KeyStoreException(java.security.KeyStoreException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CertificateEncodingException(java.security.cert.CertificateEncodingException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchProviderException(java.security.NoSuchProviderException)

Example 69 with KeySpec

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

the class BaseSecretKeyFactory method engineGetKeySpec.

protected KeySpec engineGetKeySpec(SecretKey key, Class keySpec) throws InvalidKeySpecException {
    if (keySpec == null) {
        throw new InvalidKeySpecException("keySpec parameter is null");
    }
    if (key == null) {
        throw new InvalidKeySpecException("key parameter is null");
    }
    if (SecretKeySpec.class.isAssignableFrom(keySpec)) {
        return new SecretKeySpec(key.getEncoded(), algName);
    }
    try {
        Class[] parameters = { byte[].class };
        Constructor c = keySpec.getConstructor(parameters);
        Object[] p = new Object[1];
        p[0] = key.getEncoded();
        return (KeySpec) c.newInstance(p);
    } catch (Exception e) {
        throw new InvalidKeySpecException(e.toString());
    }
}
Also used : SecretKeySpec(javax.crypto.spec.SecretKeySpec) Constructor(java.lang.reflect.Constructor) KeySpec(java.security.spec.KeySpec) SecretKeySpec(javax.crypto.spec.SecretKeySpec) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) InvalidKeyException(java.security.InvalidKeyException)

Example 70 with KeySpec

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

the class VncClient method encodePassword.

/**
     * Encode password using DES encryption with given challenge.
     *
     * @param challenge
     *            a random set of bytes.
     * @param password
     *            a password
     * @return DES hash of password and challenge
     */
public byte[] encodePassword(byte[] challenge, String password) throws Exception {
    // VNC password consist of up to eight ASCII characters.
    // Padding
    byte[] key = { 0, 0, 0, 0, 0, 0, 0, 0 };
    byte[] passwordAsciiBytes = password.getBytes(RfbConstants.CHARSET);
    System.arraycopy(passwordAsciiBytes, 0, key, 0, Math.min(password.length(), 8));
    // Flip bytes (reverse bits) in key
    for (int i = 0; i < key.length; i++) {
        key[i] = flipByte(key[i]);
    }
    KeySpec desKeySpec = new DESKeySpec(key);
    SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("DES");
    SecretKey secretKey = secretKeyFactory.generateSecret(desKeySpec);
    Cipher cipher = Cipher.getInstance("DES/ECB/NoPadding");
    cipher.init(Cipher.ENCRYPT_MODE, secretKey);
    byte[] response = cipher.doFinal(challenge);
    return response;
}
Also used : SecretKey(javax.crypto.SecretKey) DESKeySpec(javax.crypto.spec.DESKeySpec) KeySpec(java.security.spec.KeySpec) DESKeySpec(javax.crypto.spec.DESKeySpec) Cipher(javax.crypto.Cipher) SecretKeyFactory(javax.crypto.SecretKeyFactory)

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